Linux驱动开发——(八)Linux异步通知

目录

一、异步通知简介

二、信号处理

2.1 驱动程序中的处理

2.1.1 fasync_struct结构体

2.1.2 fasync操作函数

2.1.3 kill_fasync函数

2.2 应用程序中的处理

三、驱动代码


一、异步通知简介

异步通知的核心就是信号。信号类似于硬件上使用的中断,只不过信号是软件层次上的,算是在软件层次上对中断的一种模拟。

驱动可以通过主动向应用程序发送信号的方式来报告自己可以访问,应用程序获取到信号以后就可以从驱动设备中读取或者写入数据。整个过程就相当于应用程序收到了驱动发送过来了的一个“中断“,然后应用程序去响应这个”中断“。在整个处理过程中应用程序并没有去查询驱动设备是否可以访问,一切都是由驱动设备自己告诉给应用程序的。arch/xtensa/include/uapi/asm/signal.h文件中定义了Linux所支持的所有信号:

#define SIGHUP 1 /* 终端挂起或控制进程终止 */ 
#define SIGINT 2 /* 终端中断(Ctrl+C组合键) */ 
#define SIGQUIT 3 /* 终端退出(Ctrl+\组合键) */ 
#define SIGILL 4 /* 非法指令 */ 
#define SIGTRAP 5 /* debug使用,有断点指令产生 */ 
#define SIGABRT 6 /* 由abort(3)发出的退出指令 */ 
#define SIGIOT 6 /* IOT指令 */ 
#define SIGBUS 7 /* 总线错误 */ 
#define SIGFPE 8 /* 浮点运算错误 */ 
#define SIGKILL 9 /* 杀死、终止进程 */ 
#define SIGUSR1 10 /* 用户自定义信号1 */ 
#define SIGSEGV 11 /* 段违例(无效的内存段) */ 
#define SIGUSR2 12 /* 用户自定义信号2 */ 
#define SIGPIPE 13 /* 向非读管道写入数据 */ 
#define SIGALRM 14 /* 闹钟 */ 
#define SIGTERM 15 /* 软件终止 */ 
#define SIGSTKFLT 16 /* 栈异常 */ 
#define SIGCHLD 17 /* 子进程结束 */ 
#define SIGCONT 18 /* 进程继续 */ 
#define SIGSTOP 19 /* 停止进程的执行,只是暂停 */	
#define SIGTSTP 20 /* 停止进程的运行(Ctrl+Z组合键) */ 
#define SIGTTIN 21 /* 后台进程需要从终端读取数据 */ 
#define SIGTTOU 22 /* 后台进程需要向终端写数据 */ 
#define SIGURG 23 /* 有"紧急"数据 */ 
#define SIGXCPU 24 /* 超过CPU资源限制 */ 
#define SIGXFSZ 25 /* 文件大小超额 */ 
#define SIGVTALRM 26 /* 虚拟时钟信号 */ 
#define SIGPROF 27 /* 时钟信号描述 */ 
#define SIGWINCH 28 /* 窗口大小改变 */ 
#define SIGIO 29 /* 可以进行输入/输出操作 */ 
#define SIGPOLL SIGIO 
#define SIGPWR 30 /* 断点重启 */ 
#define SIGSYS 31 /* 非法的系统调用 */ 
#define SIGUNUSED 31 /* 未使用信号 */ 

在这些信号中,除了 SIGKILL(9)和 SIGSTOP(19)这两个信号不能被忽略外,其他的信号都可以忽略。驱动程序可以通过向应用程序发送不同的信号以实现不同的功能。


二、信号处理

2.1 驱动程序中的处理

2.1.1 fasync_struct结构体

首先要定义一个fasync_struct结构体指针变量:

struct fasync_struct { 
    spinlock_t fa_lock; 
    int magic; 
    int fa_fd; 

    struct fasync_struct *fa_next; 
    struct file *fa_file; 
    struct rcu_head fa_rcu; 
};
2.1.2 fasync操作函数

然后在设备驱动中实现file_operations操作集中的fasync操作函数

int (*fasync) (int fd, struct file *filp, int on)

fasync函数里一般通过调用fasync_helper函数以初始化前面定义的fasync_struct结构体指针

int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)

fasync_helper函数的前三个参数即fasync函数的三个参数,第四个参数是要初始化的fasync_struct结构体指针变量。 在关闭驱动文件的时候需要在file_operations操作集中的release操作函数中释放fasync_struct,fasync_struct的释放函数同样是fasync_helper

所以驱动中有关fasync操作函数的模板代码大致如下:

struct xxx_dev { 
    ...... 
    struct fasync_struct *async_queue; /* 异步相关结构体 */ 
}; 
 
static int xxx_fasync(int fd, struct file *filp, int on) 
{ 
    struct xxx_dev *dev = (xxx_dev)filp->private_data; 

    if (fasync_helper(fd, filp, on, &dev->async_queue) < 0) 
        return -EIO; 
    return 0; 
} 

static struct file_operations xxx_ops = { 
    ...... 
    .fasync = xxx_fasync, 
    ...... 
};

static int xxx_release(struct inode *inode, struct file *filp) 
{ 
    return xxx_fasync(-1, filp, 0); /* 删除异步通知 */ 
}
2.1.3 kill_fasync函数

当设备可以访问的时候,kill_fasync函数负责发送指定的信号,相当于产生“中断”:

void kill_fasync(struct fasync_struct **fp, int sig, int band)

fp:要操作的fasync_struct。
sig:要发送的信号。
band:可读时设置为POLL_IN,可写时设置为POLL_OUT。
返回值:无。

2.2 应用程序中的处理

应用程序应根据驱动程序所使用的信号来设置相应信号的处理函数,应用程序使用signal函数来设置信号的处理函数。然后使用fcntl(fd, F_SETOWN, getpid())将本应用程序的进程号告诉给内核以开启异步通知:

flags = fcntl(fd, F_GETFL); /* 获取当前的进程状态 */
fcntl(fd, F_SETFL, flags | FASYNC); /* 开启当前进程异步通知功能 */

重点是通过fcntl函数设置进程状态为FASYNC——驱动程序中的fasync操作函数会因此而执行。


三、驱动代码

Linux驱动开发——(六)按键中断实验的驱动代码为模板修改。

添加头文件和相关变量:

#include <linux/fcntl.h>

struct fasync_struct *async_queue;

在定时器服务函数里添加:

if(atomic_read(&dev->releasekey)) {  
    if(dev->async_queue) 
    kill_fasync(&dev->async_queue, SIGIO, POLL_IN); 
}

添加fasync操作函数:

static int imx6uirq_fasync(int fd, struct file *filp, int on) { 
    struct imx6uirq_dev *dev = (struct imx6uirq_dev *) filp->private_data; 
    
    return fasync_helper(fd, filp, on, &dev->async_queue); 
}

static struct file_operations imx6uirq_fops = { 
    .fasync = imx6uirq_fasync, 
};

完善release操作函数:

static int imx6uirq_release(struct inode *inode, struct file *filp){
    return imx6uirq_fasync(-1, filp, 0);
}

static struct file_operations imx6uirq_fops = {  
    .release = imx6uirq_release, 
};
  • 33
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值