linux字符驱动之poll机制按键驱动

在上一节中,我们讲解了如何自动创建设备节点,实现一个中断方式的按键驱动。虽然中断式的驱动,效率是蛮高的,但是大家有没有发现,应用程序的死循环里的读函数是一直在读的;在实际的应用场所里,有没有那么一种情况,偶尔有数据、偶尔没有数据,答案当然是有的。我们理想当然的就会想到,当有数据的时候,我们才去读它,没数据的时候我们读它干啥?岂不浪费劳动力?
上一节文章链接:http://blog.csdn.net/lwj103862095/article/details/17511867
这一节里,我们在中断的基础上添加poll机制来实现有数据的时候就去读,没数据的时候,自己规定一个时间,如果还没有数据,就表示超时时间。

 

poll机制总结:(韦老师总结的,不是我总结的哦!)

1. poll > sys_poll > do_sys_poll >poll_initwait,poll_initwait函数注册一下回调函数__pollwait,它就是我们的驱动程序执行poll_wait时,真正被调用的函数。

2. 接下来执行file->f_op->poll,即我们驱动程序里自己实现的poll函数

   它会调用poll_wait把自己挂入某个队列,这个队列也是我们的驱动自己定义的;

   它还判断一下设备是否就绪。

3. 如果设备未就绪,do_sys_poll里会让进程休眠一定时间

4. 进程被唤醒的条件有2:一是上面说的“一定时间”到了,二是被驱动程序唤醒。驱动程序发现条件就绪时,就把“某个队列”上挂着的进程唤醒,这个队列,就是前面通过poll_wait把本进程挂过去的队列。

5. 如果驱动程序没有去唤醒进程,那么chedule_timeout(__timeou)超时后,会重复2、3动作,直到应用程序的poll调用传入的时间到达。

 

问:这一节与上一节的在驱动里添加了哪些内容?

答:仅仅添加了poll函数,该函数如下:

 

  1. static unsigned int fourth_drv_poll(struct file *file, poll_table *wait)  
  2. {  
  3.     unsigned int mask = 0;  
  4.   
  5.     /* 该函数,只是将进程挂在button_waitq队列上,而不是立即休眠 */  
  6.     poll_wait(file, &button_waitq, wait);  
  7.   
  8.     /* 当没有按键按下时,即不会进入按键中断处理函数,此时ev_press = 0  
  9.      * 当按键按下时,就会进入按键中断处理函数,此时ev_press被设置为1 
  10.      */  
  11.     if(ev_press)  
  12.     {  
  13.         mask |= POLLIN | POLLRDNORM;  /* 表示有数据可读 */  
  14.     }  
  15.   
  16.     /* 如果有按键按下时,mask |= POLLIN | POLLRDNORM,否则mask = 0 */  
  17.     return mask;    
  18. }  


详细请参考驱动源码:

 

 

  1. #include <linux/kernel.h>  
  2. #include <linux/fs.h>  
  3. #include <linux/init.h>  
  4. #include <linux/delay.h>  
  5. #include <linux/irq.h>  
  6. #include <asm/uaccess.h>  
  7. #include <asm/irq.h>  
  8. #include <asm/io.h>  
  9. #include <linux/module.h>  
  10. #include <linux/device.h>         //class_create  
  11. #include <mach/regs-gpio.h>       //S3C2410_GPF1  
  12. //#include <asm/arch/regs-gpio.h>    
  13. #include <mach/hardware.h>  
  14. //#include <asm/hardware.h>  
  15. #include <linux/interrupt.h>  //wait_event_interruptible  
  16. #include <linux/poll.h>   //poll  
  17.   
  18.   
  19. /* 定义并初始化等待队列头 */  
  20. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);  
  21.   
  22.   
  23. static struct class *fourthdrv_class;  
  24. static struct device *fourthdrv_device;  
  25.   
  26. static struct pin_desc{  
  27.     unsigned int pin;  
  28.     unsigned int key_val;  
  29. };  
  30.   
  31. static struct pin_desc pins_desc[4] = {  
  32.         {S3C2410_GPF1,0x01},  
  33.         {S3C2410_GPF4,0x02},  
  34.         {S3C2410_GPF2,0x03},  
  35.         {S3C2410_GPF0,0x04},  
  36. };   
  37.   
  38. static int ev_press = 0;  
  39.   
  40. /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */  
  41. /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */  
  42. static unsigned char key_val;  
  43. int major;  
  44.   
  45. /* 用户中断处理函数 */  
  46. static irqreturn_t buttons_irq(int irq, void *dev_id)  
  47. {  
  48.     struct pin_desc *pindesc = (struct pin_desc *)dev_id;  
  49.     unsigned int pinval;  
  50.     pinval = s3c2410_gpio_getpin(pindesc->pin);  
  51.   
  52.     if(pinval)  
  53.     {  
  54.         /* 松开 */  
  55.         key_val = 0x80 | (pindesc->key_val);  
  56.     }  
  57.     else  
  58.     {  
  59.         /* 按下 */  
  60.         key_val = pindesc->key_val;  
  61.     }  
  62.   
  63.     ev_press = 1;                            /* 表示中断已经发生 */  
  64.      wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */  
  65.     return IRQ_HANDLED;  
  66. }  
  67. static int fourth_drv_open(struct inode * inode, struct file * filp)  
  68. {  
  69.     /*  K1 ---- EINT1,K2 ---- EINT4,K3 ---- EINT2,K4 ---- EINT0 
  70.      *  配置GPF1、GPF4、GPF2、GPF0为相应的外部中断引脚 
  71.      *  IRQT_BOTHEDGE应该改为IRQ_TYPE_EDGE_BOTH 
  72.      */  
  73.     request_irq(IRQ_EINT1, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K1",&pins_desc[0]);  
  74.     request_irq(IRQ_EINT4, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K2",&pins_desc[1]);  
  75.     request_irq(IRQ_EINT2, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K3",&pins_desc[2]);  
  76.     request_irq(IRQ_EINT0, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K4",&pins_desc[3]);  
  77.     return 0;  
  78. }  
  79.   
  80. static ssize_t fourth_drv_read(struct file *file, char __user *user, size_t size,loff_t *ppos)  
  81. {  
  82.     if (size != 1)  
  83.             return -EINVAL;  
  84.       
  85.     /* 当没有按键按下时,休眠。 
  86.      * 即ev_press = 0; 
  87.      * 当有按键按下时,发生中断,在中断处理函数会唤醒 
  88.      * 即ev_press = 1;  
  89.      * 唤醒后,接着继续将数据通过copy_to_user函数传递给应用程序 
  90.      */  
  91.     wait_event_interruptible(button_waitq, ev_press);  
  92.     copy_to_user(user, &key_val, 1);  
  93.       
  94.     /* 将ev_press清零 */  
  95.     ev_press = 0;  
  96.     return 1;     
  97. }  
  98.   
  99. static int fourth_drv_close(struct inode *inode, struct file *file)  
  100. {  
  101.     free_irq(IRQ_EINT1,&pins_desc[0]);  
  102.     free_irq(IRQ_EINT4,&pins_desc[1]);  
  103.     free_irq(IRQ_EINT2,&pins_desc[2]);  
  104.     free_irq(IRQ_EINT0,&pins_desc[3]);  
  105.     return 0;  
  106. }  
  107.   
  108. static unsigned int fourth_drv_poll(struct file *file, poll_table *wait)  
  109. {  
  110.     unsigned int mask = 0;  
  111.   
  112.     /* 该函数,只是将进程挂在button_waitq队列上,而不是立即休眠 */  
  113.     poll_wait(file, &button_waitq, wait);  
  114.   
  115.     /* 当没有按键按下时,即不会进入按键中断处理函数,此时ev_press = 0  
  116.      * 当按键按下时,就会进入按键中断处理函数,此时ev_press被设置为1 
  117.      */  
  118.     if(ev_press)  
  119.     {  
  120.         mask |= POLLIN | POLLRDNORM;  /* 表示有数据可读 */  
  121.     }  
  122.   
  123.     /* 如果有按键按下时,mask |= POLLIN | POLLRDNORM,否则mask = 0 */  
  124.     return mask;    
  125. }  
  126.   
  127.   
  128. /* File operations struct for character device */  
  129. static const struct file_operations fourth_drv_fops = {  
  130.     .owner      = THIS_MODULE,  
  131.     .open       = fourth_drv_open,  
  132.     .read       = fourth_drv_read,  
  133.     .release    = fourth_drv_close,  
  134.     .poll       = fourth_drv_poll,  
  135. };  
  136.   
  137.   
  138. /* 驱动入口函数 */  
  139. static int fourth_drv_init(void)  
  140. {  
  141.     /* 主设备号设置为0表示由系统自动分配主设备号 */  
  142.     major = register_chrdev(0, "fourth_drv", &fourth_drv_fops);  
  143.   
  144.     /* 创建fourthdrv类 */  
  145.     fourthdrv_class = class_create(THIS_MODULE, "fourthdrv");  
  146.   
  147.     /* 在fourthdrv类下创建buttons设备,供应用程序打开设备*/  
  148.     fourthdrv_device = device_create(fourthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons");  
  149.   
  150.     return 0;  
  151. }  
  152.   
  153. /* 驱动出口函数 */  
  154. static void fourth_drv_exit(void)  
  155. {  
  156.     unregister_chrdev(major, "fourth_drv");  
  157.     device_unregister(fourthdrv_device);  //卸载类下的设备  
  158.     class_destroy(fourthdrv_class);     //卸载类  
  159. }  
  160.   
  161. module_init(fourth_drv_init);  //用于修饰入口函数  
  162. module_exit(fourth_drv_exit);  //用于修饰出口函数     
  163.   
  164. MODULE_AUTHOR("LWJ");  
  165. MODULE_DESCRIPTION("Just for Demon");  
  166. MODULE_LICENSE("GPL");  //遵循GPL协议  


应用测试源码:

 

 

  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/stat.h>  
  4. #include <fcntl.h>  
  5. #include <unistd.h>  
  6. #include <poll.h>  
  7.   
  8. /* fourth_test 
  9.  */   
  10. int main(int argc ,char *argv[])  
  11.   
  12. {  
  13.     int fd;  
  14.     unsigned char key_val;  
  15.     struct pollfd fds;  
  16.     int ret;  
  17.   
  18.     fd = open("/dev/buttons",O_RDWR);  
  19.     if (fd < 0)  
  20.     {  
  21.         printf("open error\n");  
  22.     }  
  23.     fds.fd = fd;  
  24.     fds.events = POLLIN;  
  25.     while(1)  
  26.     {  
  27.         /* A value of 0 indicates  that the call timed out and no file descriptors were ready 
  28.          * poll函数返回0时,表示5s时间到了,而这段时间里,没有事件发生"数据可读" 
  29.          */  
  30.         ret = poll(&fds,1,5000);  
  31.         if(ret == 0)  
  32.         {  
  33.             printf("time out\n");  
  34.         }  
  35.         else    /* 如果没有超时,则读出按键值 */  
  36.         {  
  37.             read(fd,&key_val,1);  
  38.             printf("key_val = 0x%x\n",key_val);  
  39.         }     
  40.     }  
  41.     return 0;  
  42. }  


测试步骤:

 

 

  1. [WJ2440]# ls  
  2. Qt             etc            lib            sbin           third_test  
  3. TQLedtest      first_drv.ko   linuxrc        sddisk         tmp  
  4. app_test       first_test     mnt            second_drv.ko  udisk  
  5. bin            fourth_drv.ko  opt            second_test    usr  
  6. dev            fourth_test    proc           sys            var  
  7. driver_test    home           root           third_drv.ko   web  
  8. [WJ2440]# insmod fourth_drv.ko   
  9. [WJ2440]# lsmod  
  10. fourth_drv 3164 0 - Live 0xbf003000  
  11. [WJ2440]# ls /dev/buttons -l  
  12. crw-rw----    1 root     root      252,   0 Jan  2 03:00 /dev/buttons  
  13. [WJ2440]# ./fourth_test   
  14. time out  
  15. time out  
  16. key_val = 0x1  
  17. key_val = 0x81  
  18. key_val = 0x4  
  19. key_val = 0x84  
  20. key_val = 0x3  
  21. key_val = 0x83  
  22. key_val = 0x2  
  23. key_val = 0x82  
  24. ^C  
  25. [WJ2440]# ./fourth_test &  
  26. [WJ2440]# time out  
  27. time out  
  28. time out  
  29. [WJ2440]#top  
  30.   
  31. Mem: 10076K used, 50088K free, 0K shrd, 0K buff, 7224K cached  
  32. CPU:  0.1% usr  0.7% sys  0.0% nic 99.0% idle  0.0% io  0.0% irq  0.0% sirq  
  33. Load average: 0.00 0.00 0.00 1/23 637  
  34.   PID  PPID USER     STAT   VSZ %MEM CPU %CPU COMMAND  
  35.   637   589 root     R     2092  3.4   0  0.7 top  
  36.   589     1 root     S     2092  3.4   0  0.0 -/bin/sh  
  37.     1     0 root     S     2088  3.4   0  0.0 init  
  38.   590     1 root     S     2088  3.4   0  0.0 /usr/sbin/telnetd -l /bin/login  
  39.   587     1 root     S     1508  2.5   0  0.0 EmbedSky_wdg  
  40.   636   589 root     S     1432  2.3   0  0.0 ./fourth_test  
  41.   573     2 root     SW<      0  0.0   0  0.0 [rpciod/0]  
  42.     5     2 root     SW<      0  0.0   0  0.0 [khelper]  
  43.   329     2 root     SW<      0  0.0   0  0.0 [nfsiod]  
  44.     2     0 root     SW<      0  0.0   0  0.0 [kthreadd]  
  45.     3     2 root     SW<      0  0.0   0  0.0 [ksoftirqd/0]  
  46.     4     2 root     SW<      0  0.0   0  0.0 [events/0]  
  47.    11     2 root     SW<      0  0.0   0  0.0 [async/mgr]  
  48.   237     2 root     SW<      0  0.0   0  0.0 [kblockd/0]  
  49.   247     2 root     SW<      0  0.0   0  0.0 [khubd]  
  50.   254     2 root     SW<      0  0.0   0  0.0 [kmmcd]  
  51.   278     2 root     SW       0  0.0   0  0.0 [pdflush]  
  52.   279     2 root     SW       0  0.0   0  0.0 [pdflush]  
  53.   280     2 root     SW<      0  0.0   0  0.0 [kswapd0]  
  54.   325     2 root     SW<      0  0.0   0  0.0 [aio/0]  


由测试结果可以看出,当按键没有被按下时,5秒之后,会显示出time out,表示时间已到,在这5秒时间里,没有按键被按下,即没有数据可读,当按键按下时,立即打印出按下的按键;同时,fourth_test进程,也几乎不占用CPU的利用率。

posted @ 2017-03-09 09:04  专注it  阅读(316)  评论(0编辑  收藏  举报