有没有网站找人帮忙做图,北京提供厦门网络推广,北京到安阳的大巴,wordpress通栏目录
一、异步通知简介
二、信号处理
2.1 驱动程序中的处理
2.1.1 fasync_struct结构体
2.1.2 fasync操作函数
2.1.3 kill_fasync函数
2.2 应用程序中的处理
三、驱动代码 一、异步通知简介
异步通知的核心就是信号。信号类似于硬件上使用的中断#xff0c;只不过信号…目录
一、异步通知简介
二、信号处理
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 /* 终端中断(CtrlC组合键) */
#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 /* 停止进程的运行(CtrlZ组合键) */
#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_structfasync_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.hstruct 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,
};