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.       
  6.     poll_wait(file, &button_waitq, wait);  
  7.   
  8.       
  9.     if(ev_press)  
  10.      
  11.         mask |= POLLIN POLLRDNORM;    
  12.      
  13.   
  14.       
  15.     return mask;    
  16.  

详细请参考驱动源码:

 

 

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. #include   
  10. #include          //class_create  
  11. #include        //S3C2410_GPF1  
  12. //#include     
  13. #include   
  14. //#include   
  15. #include   //wait_event_interruptible  
  16. #include    //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.   
  41.   
  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.       
  70.     request_irq(IRQ_EINT1, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K1",&pins_desc[0]);  
  71.     request_irq(IRQ_EINT4, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K2",&pins_desc[1]);  
  72.     request_irq(IRQ_EINT2, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K3",&pins_desc[2]);  
  73.     request_irq(IRQ_EINT0, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K4",&pins_desc[3]);  
  74.     return 0;  
  75.  
  76.   
  77. static ssize_t fourth_drv_read(struct file *file, char __user *user, size_t size,loff_t *ppos)  
  78.  
  79.     if (size != 1)  
  80.             return -EINVAL;  
  81.       
  82.       
  83.     wait_event_interruptible(button_waitq, ev_press);  
  84.     copy_to_user(user, &key_val, 1);  
  85.       
  86.       
  87.     ev_press 0;  
  88.     return 1;     
  89.  
  90.   
  91. static int fourth_drv_close(struct inode *inode, struct file *file)  
  92.  
  93.     free_irq(IRQ_EINT1,&pins_desc[0]);  
  94.     free_irq(IRQ_EINT4,&pins_desc[1]);  
  95.     free_irq(IRQ_EINT2,&pins_desc[2]);  
  96.     free_irq(IRQ_EINT0,&pins_desc[3]);  
  97.     return 0;  
  98.  
  99.   
  100. static unsigned int fourth_drv_poll(struct file *file, poll_table *wait)  
  101.  
  102.     unsigned int mask 0;  
  103.   
  104.       
  105.     poll_wait(file, &button_waitq, wait);  
  106.   
  107.       
  108.     if(ev_press)  
  109.      
  110.         mask |= POLLIN POLLRDNORM;    
  111.      
  112.   
  113.       
  114.     return mask;    
  115.  
  116.   
  117.   
  118.   
  119. static const struct file_operations fourth_drv_fops  
  120.     .owner      THIS_MODULE,  
  121.     .open       fourth_drv_open,  
  122.     .read       fourth_drv_read,  
  123.     .release    fourth_drv_close,  
  124.     .poll       fourth_drv_poll,  
  125. };  
  126.   
  127.   
  128.   
  129. static int fourth_drv_init(void 
  130.  
  131.       
  132.     major register_chrdev(0, "fourth_drv"&fourth_drv_fops);  
  133.   
  134.       
  135.     fourthdrv_class class_create(THIS_MODULE, "fourthdrv");  
  136.   
  137.       
  138.     fourthdrv_device device_create(fourthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons");  
  139.   
  140.     return 0;  
  141.  
  142.   
  143.   
  144. static void fourth_drv_exit(void 
  145.  
  146.     unregister_chrdev(major, "fourth_drv");  
  147.     device_unregister(fourthdrv_device);  //卸载类下的设备  
  148.     class_destroy(fourthdrv_class);     //卸载类  
  149.  
  150.   
  151. module_init(fourth_drv_init);  //用于修饰入口函数  
  152. module_exit(fourth_drv_exit);  //用于修饰出口函数     
  153.   
  154. MODULE_AUTHOR("LWJ");  
  155. MODULE_DESCRIPTION("Just for Demon");  
  156. MODULE_LICENSE("GPL");  //遵循GPL协议  

应用测试源码:

 

 

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7.   
  8.    
  9. int main(int argc ,char *argv[])  
  10.   
  11.  
  12.     int fd;  
  13.     unsigned char key_val;  
  14.     struct pollfd fds;  
  15.     int ret;  
  16.   
  17.     fd open("/dev/buttons",O_RDWR);  
  18.     if (fd 0)  
  19.      
  20.         printf("open error\n");  
  21.      
  22.     fds.fd fd;  
  23.     fds.events POLLIN;  
  24.     while(1)  
  25.      
  26.           
  27.         ret poll(&fds,1,5000);  
  28.         if(ret == 0)  
  29.          
  30.             printf("time out\n");  
  31.          
  32.         else      
  33.          
  34.             read(fd,&key_val,1);  
  35.             printf("key_val 0x%x\n",key_val);  
  36.             
  37.      
  38.     return 0;  
  39.  

测试步骤:

 

 

  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 Live 0xbf003000  
  11. [WJ2440]# ls /dev/buttons -l  
  12. crw-rw----    root     root      252,   Jan  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         2092  3.4    0.7 top  
  36.   589     root         2092  3.4    0.0 -/bin/sh  
  37.         root         2088  3.4    0.0 init  
  38.   590     root         2088  3.4    0.0 /usr/sbin/telnetd -l /bin/login  
  39.   587     root         1508  2.5    0.0 EmbedSky_wdg  
  40.   636   589 root         1432  2.3    0.0 ./fourth_test  
  41.   573     root     SW<       0.0    0.0 [rpciod/0]  
  42.         root     SW<       0.0    0.0 [khelper]  
  43.   329     root     SW<       0.0    0.0 [nfsiod]  
  44.         root     SW<       0.0    0.0 [kthreadd]  
  45.         root     SW<       0.0    0.0 [ksoftirqd/0]  
  46.         root     SW<       0.0    0.0 [events/0]  
  47.    11     root     SW<       0.0    0.0 [async/mgr]  
  48.   237     root     SW<       0.0    0.0 [kblockd/0]  
  49.   247     root     SW<       0.0    0.0 [khubd]  
  50.   254     root     SW<       0.0    0.0 [kmmcd]  
  51.   278     root     SW        0.0    0.0 [pdflush]  
  52.   279     root     SW        0.0    0.0 [pdflush]  
  53.   280     root     SW<       0.0    0.0 [kswapd0]  
  54.   325     root     SW<       0.0    0.0 [aio/0]  

由测试结果可以看出,当按键没有被按下时,5秒之后,会显示出time out,表示时间已到,在这5秒时间里,没有按键被按下,即没有数据可读,当按键按下时,立即打印出按下的按键;同时,fourth_test进程,也几乎不占用CPU的利用率。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值