Linux异步通知
IMX6ULL嵌入式Linux驱动开发学习
以下内容是我在学习正点原子IMX6ULL开发板alpha中记录的笔记,部分摘录自正点原子IMX6ULL开发手册。
一、异步通知简介
1.1 硬件中断
中断是处理器提供的一种异步机制,配置好中断以后就可以让处理器去处理其他的事情了,当中断发生以后会触发事先设置好的中断服务函数,在中断服务函数中做具体的处理。
1.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 SIGLOS 29 */
#define SIGPWR 30 /* 断点重启 */
#define SIGSYS 31 /* 非法的系统调用 */
#define SIGUNUSED 31 /* 未使用信号 */
这些信号中,除了 SIGKILL(9)和 SIGSTOP(19)这两个信号不能被忽略外,其他的信号都可以忽略。这些信号就相当于中断号,不同的中断号代表了不同的中断,不同的中断所做的处理不同,因此,驱动程序可以通过向应用程序发送不同的信号来实现不同的功能。
1.3 信号处理函数
使用中断的时候需要设置中断处理函数,同样的,如果要在应用程序中使用信号,那么就必须设置信号所使用的信号处理函数,在应用程序中使用 signal 函数来设置指定信号的处理函数, signal 函数原型如下所示
/**
* @param signum 要设置处理函数的信号。
* @param handler 信号的处理函数。
* @return 设置成功的话返回信号的前一个处理函数,设置失败的话返回 SIG_ERR。
*/
sighandler_t signal(int signum, sighandler_t handler);
/* 信号处理函数原型 */
typedef void (*sighandler_t)(int);
1.4 驱动中对异步通知的处理
-
需要在驱动程序中定义一个
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; };一般将
fasync_struct结构体指针变量定义到设备结构体中。struct imx6uirq_dev { struct device *dev; struct class *cls; struct cdev cdev; ...... struct fasync_struct *async_queue; /* 异步相关结构体 */ }; -
要实现
file_operations里面的fasync函数。要使用异步通知,需要在设备驱动中实现
file_operations操作集中的fasync函数,格式如下所示int (*fasync) (int fd, struct file *filp, int on);fasync函数里面一般通过调用fasync_helper函数来初始化前面定义的fasync_struct结构体指针,fasync_helper函数原型如下:int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp);当应用程序通过
fcntl(fd, F_SETFL, flags | FASYNC)改变fasync标记的时候,驱动程序file_operations操作集中的fasync函数就会执行。 -
向应用程序发送信号,
kill_fasync函数当设备可以访问的时候,驱动程序需要向应用程序发出信号,相当于产生“中断”。
kill_fasync函数负责发送指定的信号,kill_fasync函数原型如下所示/** * @param fp 要操作的 fasync_struct。 * @param sig 要发送的信号 * @param band 可读时设置为 POLL_IN,可写时设置为 POLL_OUT。 */ void kill_fasync(struct fasync_struct **fp, int sig, int band); -
关闭驱动时,需要删除信号。
关闭驱动文件的时候需要在
file_operations操作集中的release函数中释放fasync_struct,fasync_struct的释放函数同样为fasync_helper。static int xxx_release(struct inode *inode, struct file *filp) { return xxx_fasync(-1, filp, 0); /* 删除异步通知 */ }
驱动中 fasync 函数参考示例
struct xxx_dev {
......
struct fasync_struct *async_queue; /* 异步相关结构体 */
};
static int xxx_release(struct inode *inode, struct file *filp)
{
return xxx_fasync(-1, filp, 0); /* 删除异步通知 */
}
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,

本文是IMX6ULL嵌入式Linux驱动开发学习笔记。介绍了异步通知,包括硬件中断、信号、信号处理函数,以及驱动和应用程序对异步通知的处理。还记录了实验驱动编写,包含驱动程序和测试APP程序,帮助理解嵌入式Linux驱动中异步通知机制。
最低0.47元/天 解锁文章
9678

被折叠的 条评论
为什么被折叠?



