字符设备之poll机制

poll机制作用:相当于一个定时器,时间到了还没有资源就唤醒进程。

主要用途就是:进程设置一段时间用来等待资源,如果时间到了资源还没有到来,进程就立刻从睡眠状态唤醒不再等待。当然这只是使用于这段时间以后资源对于该进程已经无用的情况。

内核中poll机制的实现过程:

sys_poll函数在include/linux/syscalls.h中声明

//函数定义前加宏asmlinkage ,表示这些函数通过堆栈而不是通过寄存器传递参数。
asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,long timeout);

在系统调用表arch\arm\kernel\calls.S中调用

CALL(sys_poll) //系统调用跳转表的一项

关于系统调用表的初始化在arch/arm/kernel/entry-common.S中

.equ NR_syscalls,0  //将NR_syscalls初始化为0
#define CALL(x) .equ NR_syscalls,NR_syscalls+1  //将CALL(x) 定义为: NR_syscalls = NR_syscalls  + 1
#include "calls.S"//将calls.S的内容包进来,CALL(x)上面已经有了定义,这里就相当于执行了多次NR_syscalls++,最后就统计了系统调用的个数,并对NR_syscalls进行了4的倍数对齐,这一招,特么好厉害!
#undef CALL //撤销CALL宏定义
#define CALL(x) .long x  //对CALL重新进行宏定义,也是4字节对齐
arch/arm/kernel/entry-common.S中:
sys_syscall:
		bic	scno, r0, #__NR_OABI_SYSCALL_BASE
		cmp	scno, #__NR_syscall - __NR_SYSCALL_BASE
		cmpne	scno, #NR_syscalls	@ check range
		stmloia	sp, {r5, r6}		@ shuffle args
		movlo	r0, r1
		movlo	r1, r2
		movlo	r2, r3
		movlo	r3, r4
		ldrlo	pc, [tbl, scno, lsl #2]
		b	sys_ni_syscall

最终sys_poll()函数,就相当于下面的函数:在fs/select.c文件中,SYSCALL_DEFINE3是有3个参数的系统调用的宏定义

SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,long, timeout_msecs)
{
	......

	ret = do_sys_poll(ufds, nfds, to);//调用

	......
}

好,看看应用层调用poll函数时的底层驱动执行线路

【app:】
poll();
【kernel: 】
sys_poll
	do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,struct timespec *end_time)
		poll_initwait(&table);
			init_poll_funcptr(&table->pt, __pollwait);-->pt->qproc = __pollwait; //初始化qproc函数指针,让他指向__pollwait函数
		do_poll(nfds, head, &table, end_time);
			for(;;)
			{
				for (; pfd != pfd_end; pfd++) //查询多个驱动程序
				{
					if (do_pollfd(pfd, pt))  -> mask = file->f_op->poll(file, pwait);return mask;
					{ //do_pollfd函数相当于调用驱动里面的xxx_poll函数,下面另外再进行分析,返回值mask非零,count++,记录等待事件发生的进程数
						count++;
						pt = NULL;
					}
				}

				if (count || timed_out) //若count不为0(有等待的事件发生了)或者timed_out不为0(有信号发生或超时),则推出休眠
					break;

				//上述条件不满足下面开始进入休眠,若有等待的事件发生了,超时或收到信号则唤醒
				poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)
			}

驱动里边的xxx_poll()函数分析

xxx_poll(struct file *file, poll_table *wait)
	poll_wait(file, &xxxx_waitq, wait);
//////////////////////////////////////////////////////////////////
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
	if (p && wait_address)
		p->qproc(filp, wait_address, p); //调用之前poll_initwait()函数设置的函数qproc即__pollwait,__pollwait函数只是把当前进程挂到等待队列,只是add_wait_queue(wait_address, &entry->wait);不进入休眠
}

测试驱动程序:poll_dev.c

#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/module.h>
#include <linux/device.h> 		//class_create
#include <mach/regs-gpio.h>		//S3C2440_GPF1
#include <mach/hardware.h>
#include <linux/interrupt.h>	//wait_event_interruptible
#include <linux/fs.h>
#include <linux/poll.h>			//poll

/* 定义并初始化等待队列头 */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static struct class *buttondev_class;
static struct device *buttons_device;

static struct pin_desc{
	unsigned int pin;
	unsigned int key_val;
};

static struct pin_desc pins_desc[4] = {
		{S3C2410_GPF1,0x01}, //S3C2410_GPF1是对GPF1引脚这种“设备”的编号dev_id
		{S3C2410_GPF4,0x02},
		{S3C2410_GPF2,0x03},
		{S3C2410_GPF0,0x04},
};
static int ev_press = 0;

static unsigned char key_val;
int major;

/* 中断处理函数 */
static irqreturn_t handle_irq(int irq, void *dev_id)
{
	struct pin_desc *irq_pindesc = (struct pin_desc *)dev_id;//
	unsigned int pinval;

	pinval = s3c2410_gpio_getpin(irq_pindesc->pin);//获取按键值:有按键按下返回按键值0
	/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
	/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
	if(pinval)
	{
		/* 松开 */
		key_val = 0x80 | (irq_pindesc->key_val);
	}
	else
	{
		/* 按下 */
		key_val = irq_pindesc->key_val;
	}

	ev_press = 1;							/* 表示中断已经发生 */
	wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
	return IRQ_HANDLED;
}

static int buttons_dev_open(struct inode * inode, struct file * filp)
{
	/*  K1-EINT1,K2-EINT4,K3-EINT2,K4-EINT0
  	 *  配置GPF1、GPF4、GPF2、GPF0为相应的外部中断引脚
  	 *  IRQT_BOTHEDGE应该改为IRQ_TYPE_EDGE_BOTH
	 */
	request_irq(IRQ_EINT1, handle_irq, IRQ_TYPE_EDGE_FALLING, "K1",&pins_desc[0]);
	request_irq(IRQ_EINT4, handle_irq, IRQ_TYPE_EDGE_FALLING, "K2",&pins_desc[1]);
	request_irq(IRQ_EINT2, handle_irq, IRQ_TYPE_EDGE_FALLING, "K3",&pins_desc[2]);
	request_irq(IRQ_EINT0, handle_irq, IRQ_TYPE_EDGE_FALLING, "K4",&pins_desc[3]);
	return 0;
}

static int buttons_dev_close(struct inode *inode, struct file *file)
{
	free_irq(IRQ_EINT1,&pins_desc[0]);
	free_irq(IRQ_EINT4,&pins_desc[1]);
	free_irq(IRQ_EINT2,&pins_desc[2]);
	free_irq(IRQ_EINT0,&pins_desc[3]);
	return 0;
}

static ssize_t buttons_dev_read(struct file *file, char __user *user, size_t size,loff_t *ppos)
{
	if (size != 1)
			return -EINVAL;

	/* 当没有按键按下时,休眠。
	 * 即ev_press = 0;
	 * 当有按键按下时,发生中断,在中断处理函数会唤醒
	 * 即ev_press = 1;
	 * 唤醒后,接着继续将数据通过copy_to_user函数传递给应用程序
	 */
	wait_event_interruptible(button_waitq, ev_press);
	copy_to_user(user, &key_val, 1);

	/* 将ev_press清零 */
	ev_press = 0;
	return 1;
}

//////////////////////////关键点///////////////////////////////////////
static unsigned int buttons_dev_poll(struct file *file, poll_table *wait) //该函数一旦被调用就触发poll机制
{
	unsigned int mask = 0;

	/* 该函数,只是将进程挂在button_waitq队列上,而不是立即休眠 */
	poll_wait(file, &button_waitq, wait);
	/***
	 * 假设进程还poll在上面这一函数里边,尚未超时,假设此时有中断到来,中断处理程序将ev_press置位,然后唤醒休眠队列上对应的进程
	 ***/
	/* 进程唤醒之后,立马往下执行。唤醒的可能原因:超时/中断处理 */
	if(ev_press)
	{
		mask |= POLLIN | POLLRDNORM;  /* 有数据可读 */
	}

	/* 如果有按键按下时,mask |= POLLIN | POLLRDNORM,否则mask = 0 */
	return mask;
}
///////////////////////////////////////////////////////////////////////////////

/* File operations struct for character device */
static const struct file_operations buttons_dev_fops = {
	.owner		= THIS_MODULE,
	.open		= buttons_dev_open,
	.read		= buttons_dev_read,
	.release    = buttons_dev_close,
	.poll       = buttons_dev_poll,
};

/* 驱动入口函数 */
static int buttons_dev_init(void)
{
	/* 主设备号设置为0表示由系统自动分配主设备号 */
	major = register_chrdev(0, "buttons_dev", &buttons_dev_fops);

	/* 创建buttondev类 */
	buttondev_class = class_create(THIS_MODULE, "buttondev");

	/* 在buttondev类下创建buttons设备,供应用程序打开设备*/
	buttons_device = device_create(buttondev_class, NULL, MKDEV(major, 0), NULL, "buttons");//

	return 0;
}

/* 驱动出口函数 */
static void buttons_dev_exit(void)
{
	unregister_chrdev(major, "buttons_dev");
	device_unregister(buttons_device);  //卸载类下的设备
	class_destroy(buttondev_class);		//卸载类
}

/* 模块加载和卸载函数的修饰 */
module_init(buttons_dev_init);
module_exit(buttons_dev_exit); 

MODULE_AUTHOR("CLBIAO");
MODULE_DESCRIPTION("Just for Demon");
MODULE_LICENSE("GPL");  //遵循GPL协议

测试应用程序:app_poll.c

/* 文件的编译指令是arm-linux-gcc -static -o app_poll app_poll.c */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>

/* fourth_test
 */
int main(int argc ,char *argv[])

{
	int fd;
	unsigned char key_val;
	struct pollfd fds;
	int ret;

	fd = open("/dev/buttons",O_RDWR);
	if (fd < 0)
	{
		printf("open error\n");
	}
	fds.fd = fd;//查询的文件
	fds.events = POLLIN; //期待收到poll_in值,表示有数据
	while(1)
	{
		/* A value of 0 indicates  that the call timed out and no file descriptors were ready
		 * poll函数返回0时,表示5s时间到了,而这段时间里,没有事件发生"数据可读"
		 */
		ret = poll(&fds,1,5000);
		if(ret == 0)
		{
			printf("time out\n");
		}

		else	/* 如果没有超时,则读出按键值 */
		{
			read(fd,&key_val,1);
			printf("key_val = 0x%x\n",key_val);
		}
	}
	return 0;
}

测试结果:

小结:poll流程图

时间: 2024-08-05 07:47:21

字符设备之poll机制的相关文章

浅析Linux字符设备驱动程序内核机制

前段时间在学习linux设备驱动的时候,看了陈学松著的<深入Linux设备驱动程序内核机制>一书. 说实话.这是一本非常好的书,作者不但给出了在设备驱动程序开发过程中的所须要的知识点(如对应的函数和数据结构),还深入到linux内核里去分析了这些函数或数据结构的原理.对设备驱动开发的整个过程和原理都分析的非常到位.但可能是因为知识点太多.原理也比較深的原因,这本书在知识点的排版上跨度有些大.所以读起来显得有点吃力,可是假设第一遍看的比較认真的话,再回头看第二次就真的可以非常好地理解作者的写作思

Linux高级字符设备之Poll操作

在用户程序中,select()和poll()也是与设备阻塞与非阻塞访问息息相关的,使用非阻塞I/O的应用程序通常会使用select和poll系统调用查询是否可对设备进行无阻塞的访问.select系统调用最终会引发设备驱动中的poll函数被执行. 一.select()系统调用: 用于多路监控,当没有一个文件满足要求时,select将阻塞调用进程. 1.select()原型: int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set 

Linux高级字符设备驱动 poll方法(select多路监控原理与实现)

1.什么是Poll方法,功能是什么? 2.Select系统调用(功能)      Select系统调用用于多路监控,当没有一个文件满足要求时,select将阻塞调用进程.      int select(int maxfd, fd_set *readfds, fd_set *writefds, fe_set *exceptfds, const struct timeval *timeout)     Select系统调用(参数)     1)Maxfd:           文件描述符的范围,比

字符设备驱动(六)按键poll机制

title: 字符设备驱动(六)按键poll机制 tags: linux date: 2018-11-23 18:57:40 toc: true --- 字符设备驱动(六)按键poll机制 引入 在字符设备驱动(五)按键休眠中的App中虽然使用了休眠,但是如果Read没有返回的话会一直死等,类似阻塞,我们期望等待一段时间后自动返回,等待的时候程序依然是睡眠的,这里引入poll机制 应用程序的open/close/write/read都有对应的系统内核的sys_open/sys_close/sys

LINUX设备驱动程序笔记(三)字符设备驱动程序

      <一>.主设备号和次设备号        对字符设备的访问时通过文件系统内的设备名称进行的.那些设备名称简单称之为文件系统树的节点,它们通常位于/dev目录.字符设备驱动程序的设备文件可通过ls -l命令输出的第一列中的'c'来识别.块设备同样位于/dev下,由字符'b'标识 crw-rw----  1 root root    253,   0 2013-09-11 20:33 usbmon0 crw-rw----  1 root root    253,   1 2013-09

Linux字符设备驱动框架

字符设备是Linux三大设备之一(另外两种是块设备,网络设备),字符设备就是字节流形式通讯的I/O设备,绝大部分设备都是字符设备,常见的字符设备包括鼠标.键盘.显示器.串口等等,当我们执行ls -l /dev的时候,就能看到大量的设备文件,c就是字符设备,b就是块设备,网络设备没有对应的设备文件.编写一个外部模块的字符设备驱动,除了要实现编写一个模块所需要的代码之外,还需要编写作为一个字符设备的代码. 驱动模型 Linux一切皆文件,那么作为一个设备文件,它的操作方法接口封装在struct fi

LDD3阅读笔记-字符设备驱动

主要开发流程介绍 module_init宏和module_exit宏 当模块装载时需要调用module_init宏指定的函数,卸载时需要调用 module_exit宏指定的函数 以下是简单的init流程: 初始化设备 初始化file_operation 获取字符设备号 注册字符设备 当卸载模块时,需要释放申请的设备号. 主设备号和次设备号 对字符设备的访问是通过文件系统内的设备名称进行的.那些名称被称为特殊 文件.设备文件,或者简单称为文件系统树的节点,他们通常位于/dev目录. 通常而言,主设

linux驱动程序中的poll机制编程

#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/irq.h> #include <linux/interrupt.h> #include <asm/uaccess.h> #include

Linux嵌入式驱动学习之路(二十一)字符设备驱动程序总结和块设备驱动程序的引入

字符设备驱动程序 应用程序是调用C库中的open read write等函数.而为了操作硬件,所以引入了驱动模块. 构建一个简单的驱动,有一下步骤. 1. 创建file_operations 2. 申请设备号 3. 注册字符设备驱动, 4. 驱动入口 5. 驱动出口 检查数据是否到来的方式: 1. 查询方式 2. 休眠唤醒方式 如果设备出现异常而无法唤醒时,则将永远处于休眠状态. 3. poll机制 如果没有被唤醒,则在一定时间内可自己唤醒. 4. 异步通知(信号) 而以上的几种方式通用性不高,