linux字符设备驱动-异步通知

异步通知关键步骤:

1,应用注册信号处理函数,使用signal函数;

2,谁来发:驱动发送通知信号;

3,发给谁:驱动发送通知给特定的应用程序,驱动需要知道应用程序的PID号;

4,怎么发:驱动程序使用kill_fasync函数;

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

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

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

需要添加fasync函数指针,要实现这个函数指针,在这个函数里仅仅调用下fasync——helper函数,而且这个函数是内核帮我们实现的,驱动工程师不用修改,fasync_helper函数的作用是初始化或者释放fasync_struct



例如:

struct class *drv_class;

struct device *device;

struct fasync_struct *button_fasync;

/* 定义并初始化等待队列头 */ 

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);  


  1. /* 用户中断处理函数 */  
  2. static irqreturn_t buttons_irq(int irq, void *dev_id)  
  3. {  
  4.     struct pin_desc *pindesc = (struct pin_desc *)dev_id;  
  5.     unsigned int pinval;  
  6.     pinval = s3c2410_gpio_getpin(pindesc->pin);  
  7.   
  8.     if(pinval)  
  9.     {  
  10.         /* 松开 */  
  11.         key_val = 0x80 | (pindesc->key_val);  
  12.     }  
  13.     else  
  14.     {  
  15.         /* 按下 */  
  16.         key_val = pindesc->key_val;  
  17.     }  
  18.   
  19.     ev_press = 1;                            /* 表示中断已经发生 */  
  20.     wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */  
  21.   
  22.     /* 用kill_fasync函数告诉应用程序,有数据可读了  
  23.      * button_fasync结构体里包含了发给谁(PID指定) 
  24.      * SIGIO表示要发送的信号类型 
  25.      * POLL_IN表示发送的原因(有数据可读了) 
  26.      */  
  27.     kill_fasync(&button_fasync, SIGIO, POLL_IN);  
  28.     return IRQ_HANDLED;  

  1. static ssize_t fifth_drv_read(struct file *file, char __user *user, size_t size,loff_t *ppos)  
  2. {  
  3.     if (size != 1)  
  4.             return -EINVAL;  
  5.       
  6.     /* 当没有按键按下时,休眠。 
  7.      * 即ev_press = 0; 
  8.      * 当有按键按下时,发生中断,在中断处理函数会唤醒 
  9.      * 即ev_press = 1;  
  10.      * 唤醒后,接着继续将数据通过copy_to_user函数传递给应用程序 
  11.      */  
  12.     wait_event_interruptible(button_waitq, ev_press);  
  13.     copy_to_user(user, &key_val, 1);  
  14.       
  15.     /* 将ev_press清零 */  
  16.     ev_press = 0;  
  17.     return 1;     
  18. }  

  1. static unsigned int fifth_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. }  
  19.   


/* 当应用程序调用了fcntl(fd, F_SETFL, Oflags | FASYNC);  

 * 则最终会调用驱动的fasync函数,在这里则是drv_fasync 

 * drv_fasync最终又会调用到驱动的fasync_helper函数 

 * fasync_helper函数的作用是初始化/释放fasync_struct 

 */  


static int drv_yasync(int fd,struct file *file,int on)

{

return fasync_helper(fd,file,on,&button_fasync);

}


static const struct file_operations drv_fops={

.owner=THIS_MODULE,

.open=drv_open,

.read=drv_read,

.release=drv_close,

.poll=drv_poll,

.fasync=drv_fasync,

};

/*驱动入口函数*、

static int drv_init(void)

{

 /*主设备号设置为0,表示有系统自动分配主设备号*/

major=register_chrdev(0,"char_drv",&drv_fops);

 /*创建drv类*/

drv_class=class_creat(THIS_MODULE,"drv");

/*在类下创建设备,供应用程序打开设备*、

device=device_create(drv_class,NULL,MKDEV(major,0),NULL,"button");


return 0;

}

/*驱动出口*/

static void drv_exit(void)

{

unregister_chrdev(major,"char_drv");

/*卸载类下的设备*/

device_unregister(device);

class_destory(drv_class);

}


module_init(drv_init);

module_exit(drv_exit);


MODULE_LICENSE("GPL");




应用测试程序:


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




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值