字符设备驱动--POll机制
2017-08-03 23:19
435 查看
poll函数的调用方法:Linux上执行 man poll
fds:存放需要被检测状态的Socket描述符;
nfds:用于标记数组fds中的struct pollfd结构元素的总数量;
timeout:poll函数调用阻塞的时间,单位是MS(毫秒)
理解:poll机制判断fds中的文件是否可读,可读返回可读fd个数,不可读等待timeout(时长)时间。再次判断,可读返回可读fd个数,还不可读,返回0。
(注:具体用法在后面的测试程序中)
查看pollfd的结构体
fd表示文件描述符,events表示请求检测的事件,revents表示检测之后返回的事件。
系统调用open、read、write、poll,与之对应的内核函数为:sys_open、sys_read、sys_write、sys_poll。
内核代码分析:
1.sys_poll(select.c–fs)
app执行poll函数,kernel进入sys_poll,调用 do_sys_poll(ufds, nfds, &timeout_jiffies),sys_poll对超时参数稍作处理后直接调用do_sys_poll。
2.do_sys_poll(select.c–fs)
这里poll_initwait函数初始化一个poll_wqueues变量table
去init_poll_funcpt的定义
poll_initwait (&table)> init_poll_funcptr(&pwq->pt, __pollwait);> pt->qproc = qproc;
即table->pt->qproc = __pollwait,__pollwait将在驱动的poll函数里用到。
代码从下往上分析。table->qproc = __pollwait,table里面的qproc指针指向__pollwait。
3.do_poll(select.c–fs)
①if (count || !*timeout || signal_pending(current)) break;//count非0,超时、有信号等待处理,返回应用程序
②count = wait->error;
if (count)
break;
①②的条件不满足,进程就会进入休眠,除了休眠到指定时间被系统唤醒外,还可以被驱动程序唤醒──记住这点,这就是为什么驱动的poll里要调用poll_wait的原因。
在内核中大致上实现过程:
当应用程序调用poll函数的时候,会调用到系统调用sys_poll函数,该函数最终调用do_poll函数,do_poll函数中有一个死循 环,在里面又会利用do_pollfd函数去调用驱动中的poll函数(fds中每个成员的字符驱动程序都会被扫描到),驱动程序中的Poll函数的工作 有两个,一个就是调用poll_wait 函数,把进程挂到等待队列中去(这个是必须的,你要睡眠,必须要在一个等待队列上面,否则到哪里去唤醒你呢??),另一个是确定相关的fd是否有内容可 读,如果可读,就返回1,否则返回0,如果返回1 ,do_poll函数中的count++, 然后 do_poll函数然后判断三个条件(if (count ||!timeout || signal_pending(current)))如果成立就直接跳出,如果不成立,就睡眠timeout个jiffes这么长的时间(调用schedule_timeout实现睡眠),如果在这段时间内没有其他进程去唤醒它,那么第二次执行判断的时候就会跳出死循环。如果在这段时间内有其他进程唤醒它,那么也可以跳出死循环返回(例如我们可以利用中断处理函数去唤醒它,这样的话一有数据可读,就可以让它立即返回)。【在内核中大致上实现过程–韦东山博客】
4.do_pollfd (select.c–fs)
调用我们的驱动程序里注册的poll函数。
整个流程概述:
驱动程序forth_drv.c
测试程序forthdrvtest.c
5秒内没有按键,输出超时
5秒内有按键按下,立即唤醒进程,输出键值
测试程序后台运行,查看cpu使用情况
#include <poll.h> int poll(struct pollfd *fds, nfds_t nfds, int timeout);//文件,文件数,超时时间
fds:存放需要被检测状态的Socket描述符;
nfds:用于标记数组fds中的struct pollfd结构元素的总数量;
timeout:poll函数调用阻塞的时间,单位是MS(毫秒)
理解:poll机制判断fds中的文件是否可读,可读返回可读fd个数,不可读等待timeout(时长)时间。再次判断,可读返回可读fd个数,还不可读,返回0。
(注:具体用法在后面的测试程序中)
查看pollfd的结构体
struct pollfd { int fd; /* file descriptor */ short events; /* requested events */ short revents; /* returned events */ };
fd表示文件描述符,events表示请求检测的事件,revents表示检测之后返回的事件。
系统调用open、read、write、poll,与之对应的内核函数为:sys_open、sys_read、sys_write、sys_poll。
内核代码分析:
1.sys_poll(select.c–fs)
asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds, long timeout_msecs) { s64 timeout_jiffies; if (timeout_msecs > 0) { #if HZ > 1000 /* We can only overflow if HZ > 1000 */ if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ) timeout_jiffies = -1; else #endif timeout_jiffies = msecs_to_jiffies(timeout_msecs); } else { /* Infinite (< 0) or no (0) timeout */ timeout_jiffies = timeout_msecs; } return do_sys_poll(ufds, nfds, &timeout_jiffies); }
app执行poll函数,kernel进入sys_poll,调用 do_sys_poll(ufds, nfds, &timeout_jiffies),sys_poll对超时参数稍作处理后直接调用do_sys_poll。
2.do_sys_poll(select.c–fs)
int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout) { …… poll_initwait(&table); …… fdcount = do_poll(nfds, head, &table, timeout); …… }
这里poll_initwait函数初始化一个poll_wqueues变量table
void poll_initwait(struct poll_wqueues *pwq) { init_poll_funcptr(&pwq->pt, __pollwait); pwq->error = 0; pwq->table = NULL; pwq->inline_index = 0; }
去init_poll_funcpt的定义
static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc) { pt->qproc = qproc; }
poll_initwait (&table)> init_poll_funcptr(&pwq->pt, __pollwait);> pt->qproc = qproc;
即table->pt->qproc = __pollwait,__pollwait将在驱动的poll函数里用到。
代码从下往上分析。table->qproc = __pollwait,table里面的qproc指针指向__pollwait。
3.do_poll(select.c–fs)
static int do_poll(unsigned int nfds, struct poll_list *list, struct poll_wqueues *wait, s64 *timeout) { …… for (;;) { …… if (do_pollfd(pfd, pt)) //do_pollfd(select.c--fs中)的定义, //mask = file->f_op->poll(file, pwait);return mask; //驱动的poll的定义:在Forth_drv.c的 static unsigned //forth_drv_poll(struct file *file, poll_table *wait) // 查看poll_wait定义(poll_wait(file, &button_waitq, wait)) // 知p->qproc(filp, wait_address, p); //即__pollwait(filp, button_waitq, p),查看_pollwait的定义 //作用把当前进程挂接到button_waitq队列中 //进程挂接到队列中,等待唤醒 { count++; /* 如果驱动的poll返回非0,count++ 驱动程序中forth_drv_poll if (ev_press) mask |= POLLIN | POLLRDNORM; return mask */ pt = NULL; } …… if (count || !*timeout || signal_pending(current)) break;//count非0,超时、有信号等待处理,返回应用程序 __timeout = schedule_timeout(__timeout);//休眠 __timeout 时间 count = wait->error; if (count) break; if (*timeout < 0) { /* Wait indefinitely */ __timeout = MAX_SCHEDULE_TIMEOUT; } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) { /* * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in * a loop */ __timeout = MAX_SCHEDULE_TIMEOUT - 1; *timeout -= __timeout; } else { __timeout = *timeout; *timeout = 0; } __timeout = schedule_timeout(__timeout); if (*timeout >= 0) *timeout += __timeout; } __set_current_state(TASK_RUNNING); return count; }
①if (count || !*timeout || signal_pending(current)) break;//count非0,超时、有信号等待处理,返回应用程序
②count = wait->error;
if (count)
break;
①②的条件不满足,进程就会进入休眠,除了休眠到指定时间被系统唤醒外,还可以被驱动程序唤醒──记住这点,这就是为什么驱动的poll里要调用poll_wait的原因。
在内核中大致上实现过程:
当应用程序调用poll函数的时候,会调用到系统调用sys_poll函数,该函数最终调用do_poll函数,do_poll函数中有一个死循 环,在里面又会利用do_pollfd函数去调用驱动中的poll函数(fds中每个成员的字符驱动程序都会被扫描到),驱动程序中的Poll函数的工作 有两个,一个就是调用poll_wait 函数,把进程挂到等待队列中去(这个是必须的,你要睡眠,必须要在一个等待队列上面,否则到哪里去唤醒你呢??),另一个是确定相关的fd是否有内容可 读,如果可读,就返回1,否则返回0,如果返回1 ,do_poll函数中的count++, 然后 do_poll函数然后判断三个条件(if (count ||!timeout || signal_pending(current)))如果成立就直接跳出,如果不成立,就睡眠timeout个jiffes这么长的时间(调用schedule_timeout实现睡眠),如果在这段时间内没有其他进程去唤醒它,那么第二次执行判断的时候就会跳出死循环。如果在这段时间内有其他进程唤醒它,那么也可以跳出死循环返回(例如我们可以利用中断处理函数去唤醒它,这样的话一有数据可读,就可以让它立即返回)。【在内核中大致上实现过程–韦东山博客】
4.do_pollfd (select.c–fs)
static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait) { …… if (file->f_op && file->f_op->poll) mask = file->f_op->poll(file, pwait); …… }
调用我们的驱动程序里注册的poll函数。
整个流程概述:
app:poll kernel: sys_poll do_sys_poll(ufds, nfds, &timeout_jiffies) poll_initwait(&table); init_poll_funcptr(&pwq->pt, __pollwait);//>table->qproc = __pollwait do_poll(nfds, head, &table, timeout); for (;;) { for (; pfd != pfd_end; pfd++)//查询多个驱动程序 { do_pollfd(pfd, pt)) { count++; //如果驱动的poll返回非0,count++ pt = NULL; } } if (count || !*timeout ||signal_pending(current))//count非0,超时、有信号等待处理,返回应用程序 break; __timeout = schedule_timeout(__timeout);//休眠 __timeout 时间 }
驱动程序forth_drv.c
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <linux/irq.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <asm/arch/regs-gpio.h> #include <asm/hardware.h> #include <linux/poll.h> static struct class *forthdrv_class; static struct class_device *forthdrv_class_dev; volatile unsigned long *gpfcon; volatile unsigned long *gpfdat; volatile unsigned long *gpgcon; volatile unsigned long *gpgdat; static DECLARE_WAIT_QUEUE_HEAD(button_waitq); /* 中断事件标志, 中断服务程序将它置1,forth_drv_read将它清0 */ static volatile int ev_press = 0; struct pin_desc{ unsigned int pin; unsigned int key_val; }; /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */ /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */ static unsigned char key_val; struct pin_desc pins_desc[4] = { {S3C2410_GPF0, 0x01}, {S3C2410_GPF2, 0x02}, {S3C2410_GPG3, 0x03}, {S3C2410_GPG11, 0x04}, }; /* * 确定按键值 */ static irqreturn_t buttons_irq(int irq, void *dev_id) { struct pin_desc * pindesc = (struct pin_desc *)dev_id; unsigned int pinval; pinval = s3c2410_gpio_getpin(pindesc->pin); if (pinval) { /* 松开 */ key_val = 0x80 | pindesc->key_val; } else { /* 按下 */ key_val = pindesc->key_val; } ev_press = 1; /* 表示中断发生了 */ wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */ return IRQ_RETVAL(IRQ_HANDLED); } static int forth_drv_open(struct inode *inode, struct file *file) { /* 配置GPF0,2为输入引脚 */ /* 配置GPG3,11为输入引脚 */ request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]); request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]); request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]); request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]); return 0; } ssize_t forth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) { if (size != 1) return -EINVAL; /* 如果没有按键动作, 休眠 */ wait_event_interruptible(button_waitq, ev_press); /* 如果有按键动作, 返回键值 */ copy_to_user(buf, &key_val, 1); ev_press = 0; return 1; } int forth_drv_close(struct inode *inode, struct file *file) { free_irq(IRQ_EINT0, &pins_desc[0]); free_irq(IRQ_EINT2, &pins_desc[1]); free_irq(IRQ_EINT11, &pins_desc[2]); free_irq(IRQ_EINT19, &pins_desc[3]); return 0; } static unsigned forth_drv_poll(struct file *file, poll_table *wait) { unsigned int mask = 0; poll_wait(file, &button_waitq, wait); // 不会立即休眠 if (ev_press) mask |= POLLIN | POLLRDNORM; return mask; } static struct file_operations sencod_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = forth_drv_open, .read = forth_drv_read, .release = forth_drv_close, .poll = forth_drv_poll, }; int major; static int forth_drv_init(void) { major = register_chrdev(0, "forth_drv", &sencod_drv_fops); forthdrv_class = class_create(THIS_MODULE, "forth_drv"); forthdrv_class_dev = class_device_create(forthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */ gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16); gpfdat = gpfcon + 1; gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16); gpgdat = gpgcon + 1; return 0; } static void forth_drv_exit(void) { unregister_chrdev(major, "forth_drv"); class_device_unregister(forthdrv_class_dev); class_destroy(forthdrv_class); iounmap(gpfcon); iounmap(gpgcon); return 0; } module_init(forth_drv_init); module_exit(forth_drv_exit); MODULE_LICENSE("GPL");
测试程序forthdrvtest.c
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <poll.h> /* forthdrvtest */ int main(int argc, char **argv) { int fd; unsigned char key_val; int ret;//返回值 struct pollfd fds[1];//这里只查询一个驱动程序 fd = open("/dev/buttons", O_RDWR); if (fd < 0) { printf("can't open!\n"); } fds[0].fd = fd;//查询的文件 fds[0].events = POLLIN;//查询的结果 while (1) { ret = poll(fds, 1, 5000);//文件fds,一个,5000ms,这里返回值赋给ret, if (ret == 0)/*RETURN VALUE On success, a positive number is returned; this is the number of structures which have non-zero revents fields (in other words, those descriptors with events or errors reported). A value of 0 indicates that the call timed out and no file descrip‐ tors were ready. On error, -1 is returned, and errno is set appropriately.*/ { printf("time out\n"); } else { read(fd, &key_val, 1); printf("key_val = 0x%x\n", key_val); } } return 0; }
5秒内没有按键,输出超时
5秒内有按键按下,立即唤醒进程,输出键值
测试程序后台运行,查看cpu使用情况
相关文章推荐
- 07-S3C2440驱动学习(一)嵌入式linux字符设备驱动-查询+中断+引入poll机制的按键驱动程序
- 字符设备驱动-----按键驱动(中断+poll机制)
- Linux字符设备驱动之poll机制
- 字符设备驱动-poll机制
- 嵌入式linux:字符设备驱动-----按键驱动(中断+poll机制)
- 韦东山驱动视频笔记——3.字符设备驱动程序之poll机制
- 字符设备驱动-----按键驱动(中断+poll机制)
- linux字符设备驱动-poll机制笔记
- 字符设备驱动之按键中断(POLL机制)——FS2410
- 高级字符设备驱动--中断下半部机制之workqueue(二)
- linux字符驱动之poll机制GPIO状态监测驱动
- 字符设备之poll机制
- 07-S3C2440驱动学习(一)嵌入式linux字符设备驱动-按键驱动程序之异步通知机制+原子操作+互斥信号量+阻塞与非阻塞+定时器去抖
- Linux高级字符设备驱动 poll方法(select多路监控原理与实现)
- linux字符驱动之poll机制按键驱动
- 高级字符设备驱动--中断下半部机制之tasklet(一)
- 跟着韦东山老师学习嵌入式----字符设备驱动程序之poll机制
- 字符设备驱动程序之poll机制
- 高级字符设备驱动-Poll设备方法笔记
- 字符设备之poll机制