Chinaunix首页 | 论坛 | 博客
  • 博客访问: 278866
  • 博文数量: 58
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 600
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-27 08:37
个人简介

从linux了解世界

文章分类
文章存档

2017年(5)

2016年(51)

2015年(2)

我的朋友

分类: 嵌入式

2016-01-20 12:41:37

    在用户空间中用到的poll()函数其实就是起到一个把当前进程睡眠,通过轮询等待唤醒的作用。好处是在用户态就可以让进程睡眠,而不必在写驱动不同函数时在内核态重复的实现进程的挂起和唤醒。
    驱动中poll原型:unsigned poll(struct file *file, poll_table *wait)
    应用中poll原型:int poll(struct pollfd *fds, nfds_t nfds, int timeout);
其中
struct pollfd {
    int fd;
    short events;
    short revents;
};在linux中执行man poll可以看到events为期待发生的事件,revent为真实发生的事件(这两句话从中英文都不好理解,到底怎么回事看下面的实例分析)
nfds为*fds中pollfd的个数
timeout为睡眠时间,毫秒单位。
在用户程序中使用poll函数首先系统调用sys_poll——进入内核态do_sys_poll——do_poll——do_pollfd
其中do_poll和do_pollfd最重要,在do_poll中取出最体现poll功能的框架来看:
for (;;)
{
    if (do_pollfd())
        count++;
    if (count || timed_out)
        break;
    poll_schedule_timeout();//进程开始睡眠
}
return count;
可以看出当count()大于0或者睡眠时间到了才会跳出死循环,count++执行的条件是do_pollfd()大于零,将do_pollfd最能代表功能的框架展开:
if (f.file->f_op->poll)
{
    mask = f.file->f_op->poll(f.file, pwait);
    mask &= pollfd->events | POLLERR | POLLHUP;
    pollfd->revents = mask;
    return mask;
}
当在file_operation中定义了poll,则执行我们在驱动中编写的poll,驱动中poll的返回值给mask,mask和在应用中poll传入的events| POLLERR | POLLHUP进行与运算并返回(这些宏参数换成2禁止都只有一位为1,所以只有驱动中poll的返回值和events中有相同的宏,do_pollfd的返回值才会不为零)。
下面结合用户态下的测试程序进行实例分析,在头脑中模拟运
行。下面的分析非常重要!!!!!!!

用户程序:
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|POLLOUT;
    while (1)
    {
        ret = poll(fds, 1, 5000);
        if (ret == 0)
        {
            printf("time out\n");
            printf("revents=0x%x\n",fds[0].revents);
        }
        else
        {
            read(fd, &key_val, 1);
            printf("key_val = 0x%x\n", key_val);
            printf("revents=0x%x\n",fds[0].revents);
        }
    }
    return 0;
}
运行到ret = poll(fds, 1, 5000)时,通过系统调用sys_poll——进入内核态do_sys_poll——do_poll,然后进入上文中的for(;;)死循环
for (;;)
{
    if (do_pollfd())
        count++;
    if (count || timed_out)
        break;
    poll_schedule_timeout();
}
首先执行do_pollfd,进入上文中的:
if (f.file->f_op->poll)
{
    mask = f.file->f_op->poll(f.file, pwait);//执行下文中驱动程序中的poll程序,通过poll_wait加入等待队列,假设此时没有发生中断,因此en_press等于0,驱动poll返回0,mask为0
    mask &= pollfd->events;//0和events进行与运算,mask最终为0
    pollfd->revents = mask;//revents为0,在这两句代码中可以看出events和revents的作用,在用户程序中的events和驱动poll的返回值相与,结果返回revents
    return mask;//最终do_pollfd返回0再次进入for(;;)语句
}
for (;;)
{
    if (do_pollfd()) //if不成立,count依然是0
        count++;
    if (count || timed_out)
        break;////if不成立不会跳出死循环
    poll_schedule_timeout();//开始睡眠
}
这时如果发生中断,则会执行驱动中的botton_handle,在该函数中将en_press设为1,并唤醒等待队列。这时会从poll_schedule_timeout()跳出再次进入for(;;),执行do_pollfd。
if (f.file->f_op->poll)
{
    mask = f.file->f_op->poll(f.file, pwait);//执行下文中驱动程序中的poll程序,因为en_press等于1,驱动poll返回POLLIN
    mask &= pollfd->events;//POLLIN和events(POLLIN|POLLOUT)进行与运算
    return mask;//最终do_pollfd返回POLLIN再次进入for(;;)语句
}
 for (;;)
{
    if (do_pollfd())
        count++;//如果虽然有中断唤醒,但是驱动poll的返回值和events相与为0,count还是会为0
    if (count || timed_out)
        break;//跳出死循环,count赋给用户程序poll的返回值ret
    poll_schedule_timeout();//如果虽然有中断,但count为0,不会新开始一个5s,接着中断前的计时。
}
当等待队列因为5s到了自动唤醒,再次从头运行for (;;)因为timed_out此时为1会跳出死循环,用户程序中ret为0。
PS:总结,能在用户程序ret = poll(fds, 1, 5000)后继续执行只有两种可能,自动唤醒ret为0(期间可能有中断唤醒但是events和驱动poll的返回值没有相同项),有中断唤醒并且events和驱动poll的返回值有相同项。在编写驱动程序时,脑海中要有一个框架,程序的运行过程是:用户程序poll——内核程序——驱动poll——内核程序——用户程序,所以驱动poll起到桥梁的作用。在驱动poll中要poll_wait、判断进去poll是中断进入的还是自动进入的,最后设置代表该设备文件状态的返回值。
/*****************************************************************************************/
/*****************************************************************************************/

采用中断唤醒的驱动源码(基于linux3.18,mini2440)
static volatile int ev_press = 0;//中断唤醒进程标志,0表示不是中断唤醒(信号量或者时间到自动唤醒 ),1表示中断唤醒
static char key_value;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static char keyvalue[]={0x01,0x02,0x03,0x04,0x05,0x06};
static struct class *thirddrv_class;
static struct device    *thirddrv_class_dev;

static irqreturn_t botton_handle(int irq, void *dev_id)
{
    key_value=*(char*)dev_id;
    printk("irq=%d\n",irq);
    ev_press = 1;
    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
    return IRQ_HANDLED;
}

static int third_drv_close(struct inode *inode, struct file *file)
{
    free_irq(IRQ_EINT8,&keyvalue[0]);
    free_irq(IRQ_EINT11,&keyvalue[1]);
    free_irq(IRQ_EINT13,&keyvalue[2]);
    free_irq(IRQ_EINT14,&keyvalue[3]);
    free_irq(IRQ_EINT15,&keyvalue[4]);
    free_irq(IRQ_EINT19,&keyvalue[5]);
    return 0;
}

static int third_drv_open(struct inode *inode, struct file *file)
{
    request_irq(IRQ_EINT8,botton_handle,IRQF_TRIGGER_FALLING,"bottontest1",&keyvalue[0]);
    request_irq(IRQ_EINT11,botton_handle,IRQF_TRIGGER_FALLING,"bottontest2",&keyvalue[1]);
    request_irq(IRQ_EINT13,botton_handle,IRQF_TRIGGER_FALLING,"bottontest3",&keyvalue[2]);
    request_irq(IRQ_EINT14,botton_handle,IRQF_TRIGGER_FALLING,"bottontest4",&keyvalue[3]);
    request_irq(IRQ_EINT15,botton_handle,IRQF_TRIGGER_FALLING,"bottontest5",&keyvalue[4]);
    request_irq(IRQ_EINT19,botton_handle,IRQF_TRIGGER_FALLING,"bottontest6",&keyvalue[5]);
    return 0;
}

ssize_t third_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
    if (size != 1)
    return -EINVAL;
    copy_to_user(buf, &key_value,1);
    return 1;
}

static unsigned third_drv_poll(struct file *file, poll_table *wait)
{
    unsigned int mask = 0;
    poll_wait(file, &button_waitq, wait); // 不会立即休眠,仅仅是加入队列
    if (ev_press)
        mask |= POLLIN;
    ev_press = 0;
    return mask;
}

static struct file_operations third_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   third_drv_open,     
    .read    =    third_drv_read,    
    .release =     third_drv_close,
    .poll    =  third_drv_poll,
};


int major;
static int third_drv_init(void)
{
    major = register_chrdev(0, "botton_drv", &third_drv_fops);
    thirddrv_class = class_create(THIS_MODULE, "third_drv");
    thirddrv_class_dev = device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "buttons");
    return 0;
}

static void third_drv_exit(void)
{
    unregister_chrdev(major, "third_drv");
    device_destroy(thirddrv_class,MKDEV(major, 0));
    class_destroy(thirddrv_class);
}

module_init(third_drv_init);
module_exit(third_drv_exit);
MODULE_LICENSE("GPL");  


阅读(4800) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~