zoukankan      html  css  js  c++  java
  • 《驱动学习

    1.poll机制

      上一节中使用中断的方式获取按键值,在应用程序中read的时候,如果没有按键中断,就会导致应用程序一直休眠。

      优化:使用poll机制,在休眠5秒后,如果还没有按键中断,就返回。

      linux系统再调用poll()函数时候,如果没有发生需要的事件,那么进程进入休眠。如果在限定的时间内得到需要的事件,那么成功返回,如果没有则返回超时错误信息。

      

    poll函数原型:

    int poll(struct pollfd *fds, nfds_t nfds, int timeout)

      参数: 

        fds         可以传递多个结构体,也就是说可以监测多个驱动设备所产生的事件,只要有一个产生了请求事件,就能立即返回

        struct pollfd {
              int fd;                  /* 文件描述符 */
              short events;        /* 请求的事件类型,监视驱动文件的事件掩码 */
              short revents;       /* 驱动文件实际返回的事件 */
        } ;

        nfds       监测驱动文件的个数

        timeout  超时时间,单位为ms 

      事件类型events 可以为下列值:

    •   POLLIN           有数据可读
    •   POLLRDNORM 有普通数据可读,等效与POLLIN
    •   POLLPRI         有紧迫数据可读
    •   POLLOUT        写数据不会导致阻塞
    •   POLLER          指定的文件描述符发生错误
    •   POLLHUP        指定的文件描述符挂起事件
    •   POLLNVAL      无效的请求,打不开指定的文件描述符

       返回值:

      有事件发生        返回revents域不为0的文件描述符个数(也就是说事件发生,或者错误报告)

      超时                   返回0;

      失败              返回-1,并设置errno为错误类型

    2.解析poll函数

      poll函数对应的系统调用:sys_poll

      在老的内核中是:sys_poll,可以找的到相对应的源码。

      在新的内核中是:SYSCALL_DEFINE3(),是一个宏定义。具体后面有时间再分析

      sys_poll之后会调用do_sys_poll

    int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
            struct timespec *end_time)
    {
            ............. 略
        poll_initwait(&table);
        fdcount = do_poll(nfds, head, &table, end_time);
            .............略
    }

      poll_initwait函数

    void poll_initwait(struct poll_wqueues *pwq)
    {
        init_poll_funcptr(&pwq->pt, __pollwait);
        pwq->polling_task = current;
        pwq->triggered = 0;
        pwq->error = 0;
        pwq->table = NULL;
        pwq->inline_index = 0;
    }

      只是将table进行一个初始化操作

      poll_initwait->

        init_poll_funcptr

    static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
    {
        pt->qproc = qproc;
        pt->key   = ~0UL; /* all events enabled */
    }

      所以table->pt->qproc = qproc。qproc = __pollwait。是一个函数指针。在后面驱动程序中会调用。

      pollwait函数:

    static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
                    poll_table *p)
    {
        struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt);
        struct poll_table_entry *entry = poll_get_entry(pwq);
        if (!entry)
            return;
        get_file(filp);
        entry->filp = filp;
        entry->wait_address = wait_address;
        entry->key = p->key;
        init_waitqueue_func_entry(&entry->wait, pollwake);
        entry->wait.private = pwq;
        add_wait_queue(wait_address, &entry->wait);
    }

      接下来分析do_poll函数:

    static int do_poll(unsigned int nfds,  struct poll_list *list,
         struct poll_wqueues *wait, struct timespec *end_time)
    {
     /* 略 */
     for (;;) {
       /*略*/
       for (; pfd != pfd_end; pfd++) {   /*可以查询多个文件*/
        if (do_pollfd(pfd, pt)) {  //实际调用mask = file->f_op->poll(file, pwait); return mask;
                 //其中的poll也就是file_operations中的poll,也就是调用驱动程序中的poll
           count++;               //当do_pollfd返回的是非0的mask,那么count就++
           pt = NULL;
        }
       }
      }
      
      //跳出死循环的条件,count非0或者timed_out=1即超时。count非0就代表着驱动程序中的poll函数中的mask返回中断触发值
      if (count || timed_out)
       break;    //break之后,就会回到应用程序
      //之后就进入休眠,然后休眠完成后,timed_out=1,回到for死循环。然后break,回到应用程序。
      if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
       timed_out = 1;
     }
     return count;
    }

    3.poll驱动程序

    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;
    }

      poll_wait的时候,只是调用了p->qproc(filp, wait_address, p),也就是调用了__pollwait函数。把当前的进程挂到button_waitq队列里去,它并不会休眠。

      那么什么时候会进入休眠呢?

      在执行do_poll的时候,当运行poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)就会进入休眠。

      在哪里唤醒休眠?

      在中断服务函数中。

    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);
    }

      总结:

      当应用程序调用poll函数,就会调用sys_poll,然后调用do_sys_poll。

        在do_sys_poll中有两个相关函数:poll_initwait和do_poll

          poll_initwait主要作用初始化,然后将__pollwait函数赋给一个函数指针,然后再poll驱动程序中可以使用,作用就是将进程添加到队列。

          do_poll主要在一个死循环中:

            根据do_pollfd去调用file->f_op->poll(file, pwait),其实也就是调用file_operations结构体中的poll,也就是我们编写的poll驱动程序。

            根据poll驱动程序返回的值mask,如果非0,则执行poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)进入休眠。

            所以poll驱动程序是不进入休眠,只是将进程添加到队列中。

      注意:poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)的第二个参数为TASK_INTERRUPTIBLE,表示它总是让进程休眠到超时(如果设置了超时)为止,并可被中断。

        

      唤醒休眠:

        1.当发生中断的时候。由于我们在中断服务程序中调用了唤醒函数,则程序就从刚休眠的地方poll_schedule_timeout运行,因为不是超时引起的,则timed_out不会被置为1,

         然后重复死循环,进入do_pollfd函数调用poll驱动程序。由于当中断服务程序中已经将ev_press置为1,在poll驱动程序中mask被赋值为 mask |= POLLIN | POLLRDNORM;

         然后do_poll中的count++,然后break出死循环,回到应用程序。

        2.没用发生中断直到超时。也就是poll_schedule_timeout一直休眠,然后超时导致timed_out置1,然后重复死循环的时候,通过if (count || timed_out)条件判断,break出死循环。

        

     5.完成的代码

      

    #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;
    
    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;
    
    /*
     * K1,K2,K3,K4对应GPG0,GPG3,GPG5,GPG6
     */
    
    struct pin_desc pins_desc[4] = {
        {S3C2410_GPG0, 0x01},
        {S3C2410_GPG3, 0x02},
        {S3C2410_GPG5, 0x03},
        {S3C2410_GPG6, 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)
    {
        /* GPG0,GPG3,GPG5,GPG6为中断引脚: EINT8,EINT11,EINT13,EINT14 */
        request_irq(IRQ_EINT8,  buttons_irq, IRQT_BOTHEDGE, "K1", &pins_desc[0]);
        request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "K2", &pins_desc[1]);
        request_irq(IRQ_EINT13, buttons_irq, IRQT_BOTHEDGE, "K3", &pins_desc[2]);
        request_irq(IRQ_EINT14, buttons_irq, IRQT_BOTHEDGE, "K4", &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_EINT8,  &pins_desc[0]);
        free_irq(IRQ_EINT11, &pins_desc[1]);
        free_irq(IRQ_EINT13, &pins_desc[2]);
        free_irq(IRQ_EINT14, &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;
    
        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);
        return 0;
    }
    
    
    module_init(forth_drv_init);
    
    module_exit(forth_drv_exit);
    
    MODULE_LICENSE("GPL");
    #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!
    ");
        }
    
        fds[0].fd     = fd;
        fds[0].events = POLLIN;
        while (1)
        {
            ret = poll(fds, 1, 5000);
            if (ret == 0)
            {
                printf("time out
    ");
            }
            else
            {
                read(fd, &key_val, 1);
                printf("key_val = 0x%x
    ", key_val);
            }
        }
        
        return 0;
    }
  • 相关阅读:

    Android自己主动化測试之Monkeyrunner用法及实例
    递归函数的概念使用方法与实例
    正则、grep、sed、awk
    我的java学习笔记(一)
    mysql经常使用命令总结
    JSP动作--JSP有三种凝视方式
    http长连接和短连接
    StirngUtil工具类 之 邮箱注冊 域名不区分大写和小写方法
    在Eclipse上搭建Cocos2d-x的Android开发环境
  • 原文地址:https://www.cnblogs.com/zhuangquan/p/11636349.html
Copyright © 2011-2022 走看看