Linux驱动之poll机制


 

上一小节写到的中断方式获取按键值时,应用程序需要不停的查询,占用CPU资源太多,现在改用poll机制实现相同的按键效果。
1. poll机制流程

    当应用程序调用poll函数的时候(比如:poll(fds,1,5000),fds是定义的一个poll类型的结构体),poll是一个系统调用,会调用到内核入口函数sys_poll(可在内核中找到该函数),该函数最终会调用do_poll函数,do_poll函数中有一个死循环,在里面又会利用do_pollfd()函数去调用驱动中file_operations里的.poll函数。do_poll函数流程代码如下:

    do_poll(...)
    {
        for (;;)
        {
            set_current_state(TASK_INTERRUPTIBLE);    //设置当前任务状态为TASK_INTERRUPTIBLE
            ...
            for()
            {
                if(do_pollfd(pfd, pt))  //会调用到内核里file_operations里的.poll函数
                  {
                      count++;
                    pt = NULL;
                  }
            }
            ...
            if(count || !*timeout || signal_pending(current))
                break;
            ...
            __timeout = *timeout;
            *timeout = 0;
            ...
            __timeout= schedule_timeout(__timeout); // 休眠时间从应用程序传入
            ...
            *timeout += __timeout;
            ...
        }
        __set_current_state(TASK_RUNNING);    //设置当前任务状态为TASK_INTERRUPTIBLE
        return count;
    }

   当进入do_poll()死循环函数时,首先设置当前进程为TASK_INTERRUPTIBLE状态(该状态下的进程如果休眠的话可以被信号和wake_up()唤醒),然后执行到第一个if()语句do_pollfd(pfd, pt),里面实际调用到对应驱动设备里file_operations里的.poll函数,该函数又会调用poll_wait()函数将该进程加入休眠队列(此时没有进入休眠)。
    驱动程序里的poll函数此时应该返回给if()函数为0,不执行if()语句里的内容,继续往下执行,第二个if()的3个判断条件都不满足所以也不执行break,最后调用schedule_timeout()这个休眠函数,进入休眠。
    接下来可能有两种情况:
    (1) 超时退出休眠
    程序调用schedule_timeout()进行休眠的时间到了,schedule_timeout()返回0,重新开始死循环,跑到第二个if()语句时满足判断条件*timeout为0,执行break退出死循环。调用poll系统调用的的应用程序继续往下执行。
    (2) 被wake_up()唤醒休眠
    在schedule_timeout()休眠时间没到时被wake_up()唤醒,此时重新执行for死循环,当循环到第一个if()语句时,do_pollfd(pfd, pt)函数(也就是驱动中file_operations里的.poll函数)如果返回非0值,满足条件执行count++,当执行到下一个if()语句也就满足条件执行break退出死循环。调用poll系统调用的的应用程序继续往下执行。

    因此,我们还知道应用程序调用poll有两种返回值,返回的count为1时表示对应驱动设备里file_operations里的.poll函数返回非0值,返回值为0时表示count并没有自增而是超时时间到了才退出死循环的)。当驱动程序没写.poll函数时,应用程序也能调用系统调用poll函数,此时只充当简单的延时作用。
2. 编写代码

驱动程序poll_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>
    #include <linux/cdev.h>
     
    int major;
    static struct cdev button_cdev;
    static struct class *button_class;
     
    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 button_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 button_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
    {
        int result;
        if (size != 1)
            return -EINVAL;
     
        /* 如果没有按键动作, 休眠 */
        wait_event_interruptible(button_waitq, ev_press);
     
        /* 如果有按键动作, 返回键值 */
        result=copy_to_user(buf, &key_val, 1);
        ev_press = 0;
        
        return result;
    }
     
    int button_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 button_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 button_fops = {
        .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
        .open    =  button_open,     
        .read     =    button_read,       
        .release =  button_close,
        .poll    =  button_poll,
    };
     
    static int button_init(void)
    {
        int result;
        dev_t devid = MKDEV(major, 0);    //从主设备号major,次设备号0得到dev_t类型
        if (major)
        {
            result=register_chrdev_region(devid, 1, "button");    //注册字符设备
        }
        else
        {
            result=alloc_chrdev_region(&devid, 0, 1, "button");    //注册字符设备
            major = MAJOR(devid);        //从dev_t类型得到主设备
        }
        if(result<0)
            return result;
        
        cdev_init(&button_cdev, &button_fops);
        cdev_add(&button_cdev, devid, 1);
     
        button_class = class_create(THIS_MODULE, "button_drv");
     
        class_device_create(button_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 button_exit(void)
    {
        class_device_destroy(button_class, MKDEV(major, 0));
        class_destroy(button_class);
        cdev_del(&button_cdev);
        unregister_chrdev_region(MKDEV(major, 0), 1);
        
        iounmap(gpfcon);
        iounmap(gpgcon);
        return 0;
    }
     
    module_init(button_init);
    module_exit(button_exit);
    MODULE_LICENSE("GPL");
     

应用程序poll_test.c完整代码如下:

     
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <poll.h>
     
    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);
            if (ret == 0)
            {
                printf("time out\n");
            }
            else
            {
                read(fd, &key_val, 1);
                printf("key_val = 0x%x\n", key_val);
            }
        }
        
        return 0;
    }
     

Makefile如下:

    KERN_DIR = /work/system/linux-2.6.22.6    //内核目录
     
    all:
        make -C $(KERN_DIR) M=`pwd` modules
     
    clean:
        make -C $(KERN_DIR) M=`pwd` modules clean
        rm -rf modules.order
     
    obj-m    += poll_drv.o

3. 测试

内核:linux-2.6.22.6
编译器:arm-linux-gcc-3.4.5
环境:ubuntu9.10

将button_drv.c、button_test.c、Makefile三个文件放入网络文件系统内,在ubuntu该目录下执行:
    make
    arm-linux-gcc -o poll_test poll_test.c
在挂载了网络文件系统的开发板上进入相同目录,执行“ls”查看:

装载驱动:
    insmod poll_drv.ko
运行测试程序:
    ./poll_test
按下开发板的4个按键,结果如下:

长时间未操作,会打印time_out.
4. 程序说明

    当执行应用程序时,首先打开/dev/buttons设备,接着进入死循环调用poll(fds, 1, 5000)(第三个参数为休眠时间5秒),系统调用sys_poll最后调用到do_poll函数(死循环函数)里陷入休眠(休眠前先执行了一次驱动里的button_poll函数),当有按键按下时,调用wake_up_interruptible()唤醒休眠,重新循环执行do_poll函数第一个if()函数的判断语句,此时button_poll函数返回非0值,执行count++,再往下执行第二个if()语句break退出循环(即退出应用程序里的poll(fds, 1, 5000)),此时再将键值读出来。当5秒内没有操作按键时,也会退出poll(fds, 1, 5000),打印time_out。
5. 知识点
poll相关函数

    与.write、.read、.open等函数一样,需要在file_operations里面填充.poll    =  button_poll,,在button_poll函数里面需要调用poll_wait,函数原型如下:

static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p);

参数:
filp:设备文件信息的 struct file 结构体的指针参数。
wait_address:等待队列头。
p:追加到设备驱动上的 poll_table结构体指针参数。
    调用完该函数时,进程并没有立即进入休眠,只是将进程加入wait_address队列。
PS:wake_up的参数就是想要唤醒的队列,这里就是poll_wait传入的队列。
————————————————
版权声明:本文为优快云博主「墨、白」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/qq_36576792/article/details/84321872

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值