linux字符驱动之异步通知按键驱动

在上一节里,我们在中断的基础上添加poll机制来实现有数据的时候就去读,没数据的时候,自己规定一个时间,如果还没有数据,就表示超时时间。在此以前,我们都是让应用程序主动去读,那有没有一种情况,当驱动程序有数据时,主动去告诉应用程序,告诉它,有数据了,你赶紧来读吧。答案当然是有的,这种情况在Linux里的专业术语就叫异步通知。

上一节文章链接:http://blog.csdn.net/u013491946/article/details/72886234

在这一节里,我们将在上一节的基础上修改驱动,将其修改为有异步通知功能的按键驱动,目标:按下按键时,驱动主动去通知应用程序。

问:如何实现异步通知,有哪些要素?

答:有四个要素:

一、应用程序要实现有:注册信号处理函数,使用signal函数

二、谁来发?驱动来发

三、发给谁?发给应用程序,但应用程序必须告诉驱动PID

四、怎么发?驱动程序使用kill_fasync函数

问:应该在驱动的哪里调用kill_fasync函数?

答:kill_fasync函数的作用是,当有数据时去通知应用程序,理所当然的应该在用户终端处理函数里调用。

问:file_operations需要添加什么函数指针成员吗?

答:要的,需要添加fasync函数指针,要实现这个函数指针,幸运的是,这个函数仅仅调用了fasync_helper函数,而且这个函数是内核帮我们实现好了,驱动工程师不用修改,fasync_helper函数的作用是初始化/释放fasync_struct


详细请参考驱动源码:

[cpp]  view plain  copy
 print ?
  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. #include <linux/fcntl.h>  
  18.   
  19.   
  20. /* 定义并初始化等待队列头 */  
  21. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);  
  22.   
  23.   
  24. static struct class *fifthdrv_class;  
  25. static struct device *fifthdrv_device;  
  26.   
  27. static struct pin_desc{  
  28.     unsigned int pin;  
  29.     unsigned int key_val;  
  30. };  
  31.   
  32. static struct pin_desc pins_desc[4] = {  
  33.         {S3C2410_GPF1,0x01},  
  34.         {S3C2410_GPF4,0x02},  
  35.         {S3C2410_GPF2,0x03},  
  36.         {S3C2410_GPF0,0x04},  
  37. };   
  38.   
  39. static int ev_press = 0;  
  40.   
  41. /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */  
  42. /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */  
  43. static unsigned char key_val;  
  44. int major;  
  45.   
  46. static struct fasync_struct *button_fasync;  
  47.   
  48. /* 用户中断处理函数 */  
  49. static irqreturn_t buttons_irq(int irq, void *dev_id)  
  50. {  
  51.     struct pin_desc *pindesc = (struct pin_desc *)dev_id;  
  52.     unsigned int pinval;  
  53.     pinval = s3c2410_gpio_getpin(pindesc->pin);  
  54.   
  55.     if(pinval)  
  56.     {  
  57.         /* 松开 */  
  58.         key_val = 0x80 | (pindesc->key_val);  
  59.     }  
  60.     else  
  61.     {  
  62.         /* 按下 */  
  63.         key_val = pindesc->key_val;  
  64.     }  
  65.   
  66.     ev_press = 1;                            /* 表示中断已经发生 */  
  67.     wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */  
  68.   
  69.     /* 用kill_fasync函数告诉应用程序,有数据可读了  
  70.      * button_fasync结构体里包含了发给谁(PID指定) 
  71.      * SIGIO表示要发送的信号类型 
  72.      * POLL_IN表示发送的原因(有数据可读了) 
  73.      */  
  74.     kill_fasync(&button_fasync, SIGIO, POLL_IN);  
  75.     return IRQ_HANDLED;  
  76. }  
  77. static int fifth_drv_open(struct inode * inode, struct file * filp)  
  78. {  
  79.     /*  K1 ---- EINT1,K2 ---- EINT4,K3 ---- EINT2,K4 ---- EINT0 
  80.      *  配置GPF1、GPF4、GPF2、GPF0为相应的外部中断引脚 
  81.      *  IRQT_BOTHEDGE应该改为IRQ_TYPE_EDGE_BOTH 
  82.      */  
  83.     request_irq(IRQ_EINT1, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K1",&pins_desc[0]);  
  84.     request_irq(IRQ_EINT4, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K2",&pins_desc[1]);  
  85.     request_irq(IRQ_EINT2, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K3",&pins_desc[2]);  
  86.     request_irq(IRQ_EINT0, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K4",&pins_desc[3]);  
  87.     return 0;  
  88. }  
  89.   
  90. static ssize_t fifth_drv_read(struct file *file, char __user *user, size_t size,loff_t *ppos)  
  91. {  
  92.     if (size != 1)  
  93.             return -EINVAL;  
  94.       
  95.     /* 当没有按键按下时,休眠。 
  96.      * 即ev_press = 0; 
  97.      * 当有按键按下时,发生中断,在中断处理函数会唤醒 
  98.      * 即ev_press = 1;  
  99.      * 唤醒后,接着继续将数据通过copy_to_user函数传递给应用程序 
  100.      */  
  101.     wait_event_interruptible(button_waitq, ev_press);  
  102.     copy_to_user(user, &key_val, 1);  
  103.       
  104.     /* 将ev_press清零 */  
  105.     ev_press = 0;  
  106.     return 1;     
  107. }  
  108.   
  109. static int fifth_drv_close(struct inode *inode, struct file *file)  
  110. {  
  111.     free_irq(IRQ_EINT1,&pins_desc[0]);  
  112.     free_irq(IRQ_EINT4,&pins_desc[1]);  
  113.     free_irq(IRQ_EINT2,&pins_desc[2]);  
  114.     free_irq(IRQ_EINT0,&pins_desc[3]);  
  115.     return 0;  
  116. }  
  117.   
  118. static unsigned int fifth_drv_poll(struct file *file, poll_table *wait)  
  119. {  
  120.     unsigned int mask = 0;  
  121.   
  122.     /* 该函数,只是将进程挂在button_waitq队列上,而不是立即休眠 */  
  123.     poll_wait(file, &button_waitq, wait);  
  124.   
  125.     /* 当没有按键按下时,即不会进入按键中断处理函数,此时ev_press = 0  
  126.      * 当按键按下时,就会进入按键中断处理函数,此时ev_press被设置为1 
  127.      */  
  128.     if(ev_press)  
  129.     {  
  130.         mask |= POLLIN | POLLRDNORM;  /* 表示有数据可读 */  
  131.     }  
  132.   
  133.     /* 如果有按键按下时,mask |= POLLIN | POLLRDNORM,否则mask = 0 */  
  134.     return mask;    
  135. }  
  136.   
  137. /* 当应用程序调用了fcntl(fd, F_SETFL, Oflags | FASYNC);  
  138.  * 则最终会调用驱动的fasync函数,在这里则是fifth_drv_fasync 
  139.  * fifth_drv_fasync最终又会调用到驱动的fasync_helper函数 
  140.  * fasync_helper函数的作用是初始化/释放fasync_struct 
  141.  */  
  142. static int fifth_drv_fasync(int fd, struct file *filp, int on)  
  143. {  
  144.     return fasync_helper(fd, filp, on, &button_fasync);  
  145. }  
  146.   
  147. /* File operations struct for character device */  
  148. static const struct file_operations fifth_drv_fops = {  
  149.     .owner      = THIS_MODULE,  
  150.     .open       = fifth_drv_open,  
  151.     .read       = fifth_drv_read,  
  152.     .release    = fifth_drv_close,  
  153.     .poll       = fifth_drv_poll,  
  154.     .fasync     = fifth_drv_fasync,  
  155. };  
  156.   
  157.   
  158. /* 驱动入口函数 */  
  159. static int fifth_drv_init(void)  
  160. {  
  161.     /* 主设备号设置为0表示由系统自动分配主设备号 */  
  162.     major = register_chrdev(0, "fifth_drv", &fifth_drv_fops);  
  163.   
  164.     /* 创建fifthdrv类 */  
  165.     fifthdrv_class = class_create(THIS_MODULE, "fifthdrv");  
  166.   
  167.     /* 在fifthdrv类下创建buttons设备,供应用程序打开设备*/  
  168.     fifthdrv_device = device_create(fifthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons");  
  169.   
  170.     return 0;  
  171. }  
  172.   
  173. /* 驱动出口函数 */  
  174. static void fifth_drv_exit(void)  
  175. {  
  176.     unregister_chrdev(major, "fifth_drv");  
  177.     device_unregister(fifthdrv_device);  //卸载类下的设备  
  178.     class_destroy(fifthdrv_class);      //卸载类  
  179. }  
  180.   
  181. module_init(fifth_drv_init);  //用于修饰入口函数  
  182. module_exit(fifth_drv_exit);  //用于修饰出口函数      
  183.   
  184. MODULE_AUTHOR("LWJ");  
  185. MODULE_DESCRIPTION("Just for Demon");  
  186. MODULE_LICENSE("GPL");  //遵循GPL协议  

应用测试程序源码:

[cpp]  view plain  copy
 print ?
  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/stat.h>  
  4. #include <fcntl.h>  
  5. #include <unistd.h>   //sleep  
  6. #include <poll.h>  
  7. #include <signal.h>  
  8. #include <fcntl.h>  
  9.   
  10. int fd;  
  11.   
  12. void mysignal_fun(int signum)  
  13. {  
  14.     unsigned char key_val;  
  15.     read(fd,&key_val,1);  
  16.     printf("key_val = 0x%x\n",key_val);  
  17. }  
  18.   
  19.   
  20. /* fifth_test 
  21.  */   
  22. int main(int argc ,char *argv[])  
  23. {  
  24.     int flag;  
  25.     signal(SIGIO,mysignal_fun);  
  26.   
  27.     fd = open("/dev/buttons",O_RDWR);  
  28.     if (fd < 0)  
  29.     {  
  30.         printf("open error\n");  
  31.     }  
  32.   
  33.     /* F_SETOWN:  Set the process ID 
  34.      *  告诉内核,发给谁 
  35.      */  
  36.     fcntl(fd, F_SETOWN, getpid());  
  37.   
  38.     /*  F_GETFL :Read the file status flags 
  39.      *  读出当前文件的状态 
  40.      */  
  41.     flag = fcntl(fd,F_GETFL);  
  42.   
  43.     /* F_SETFL: Set the file status flags to the value specified by arg 
  44.      * int fcntl(int fd, int cmd, long arg); 
  45.      * 修改当前文件的状态,添加异步通知功能 
  46.      */  
  47.     fcntl(fd,F_SETFL,flag | FASYNC);  
  48.       
  49.     while(1)  
  50.     {  
  51.         /* 为了测试,主函数里,什么也不做 */  
  52.         sleep(1000);  
  53.     }  
  54.     return 0;  
  55. }  

测试步骤:

[cpp]  view plain  copy
 print ?
  1. [WJ2440]# ls   
  2. Qt             fifth_drv.ko   lib            sddisk         udisk  
  3. TQLedtest      fifth_test     linuxrc        second_drv.ko  usr  
  4. app_test       first_drv.ko   mnt            second_test    var  
  5. bin            first_test     opt            sys            web  
  6. dev            fourth_drv.ko  proc           third_drv.ko  
  7. driver_test    fourth_test    root           third_test  
  8. etc            home           sbin           tmp  
  9. [WJ2440]# insmod fifth_drv.ko   
  10. [WJ2440]# lsmod  
  11. fifth_drv 3360 0 - Live 0xbf006000  
  12. [WJ2440]# ls /dev/buttons -l  
  13. crw-rw----    1 root     root      252,   0 Jan  2 04:27 /dev/buttons  
  14. [WJ2440]# ./fifth_test   
  15. key_val = 0x1  
  16. key_val = 0x81  
  17. key_val = 0x4  
  18. key_val = 0x84  
  19. key_val = 0x2  
  20. key_val = 0x82  
  21. key_val = 0x3  
  22. key_val = 0x83  
  23. key_val = 0x4  
  24. key_val = 0x84  
  25. key_val = 0x84  

由测试可知,当无按键按下时,应用测试程序一直在sleep,当有按键按下时,signal会被调用,最终会调用mysignal_fun,在此函数里read(fd,&key_val,1);会去读出按键值,这样一来,应用程序就相当于不用主动去读数据了,每当驱动里有数据时,就会告诉应用程序有数据了,你该去读数据了,此时read函数才会被调用。


这里最后总结一下韦老师的笔记:

为了使设备支持异步通知机制,驱动程序中涉及以下3项工作:
1. 支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应进程ID。
   不过此项工作已由内核完成,设备驱动无须处理。
2. 支持F_SETFL命令的处理,每当FASYNC标志改变时,驱动程序中的fasync()函数将得以执行。
   驱动中应该实现fasync()函数。
3. 在设备资源可获得时,调用kill_fasync()函数激发相应的信号

应用程序:
fcntl(fd, F_SETOWN, getpid());  // 告诉内核,发给谁

Oflags = fcntl(fd, F_GETFL);   
fcntl(fd, F_SETFL, Oflags | FASYNC);  // 改变fasync标记,最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值