异步通知
阻塞或者非阻塞的方式来读取驱动中按键值都是应用程序主动读取的,对于非阻塞方式来说还需要应用程序通过poll函数不断的轮询。最好的方式就是驱动程序能主动向应用程序发出通知,报告自己可以访问,然后应用程序在从驱动程序中读取或写入数据,类似于裸机的中断。Linux提供了异步通知这个机制来完成此功能, 异步通知的核心就是信号,在 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)这两个信号不能被忽略外,其他的信号都可以忽略
应用程序对异步通知处理
static void sigio_signal_func(int signum)
{
int err = 0;
unsigned int keyvalue = 0;
err = read(fd, &keyvalue, sizeof(keyvalue));
if(err < 0) {
/* 读取错误 */
} else {
printf("sigio signal! key value=%d\r\n", keyvalue);
}
}
/*
* 1.注册信号SIGIO的处理函数
*/
signal(SIGIO, sigio_signal_func);
/*
* 2.将本应用程序的进程号告诉给内核
*/
fcntl(fd, F_SETOWN, getpid());
/*
* 3.开启异步通知
*/
flags = fcntl(fd, F_GETFD); /* 获取当前的进程状态 */
fcntl(fd, F_SETFL, flags | FASYNC); /* fcntl函数设置进程状态为 FASYNC,经过这一步,驱动程序中的 fasync函数就会执行。*/
注册signal函数原型如下所示
/*
*@功能 :
*@signum :要设置处理函数的信号
*@handler :信号的处理函数 typedef void (*sighandler_t)(int)
*@返回值 :设置成功的话返回信号的前一个处理函数,设置失败的话返回 SIG_ERR。
*/
sighandler_t signal(int signum, sighandler_t handler)
驱动中信号处理
// 定义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;
};
int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
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) /*初始化fasync_struct结构体*/
return -EIO;
return 0;
}
static int xxx_release(struct inode *inode, struct file *filp)
{
return xxx_fasync(-1, filp, 0); /* 删除异步通知 */
}
static struct file_operations xxx_ops = {
......
.fasync = xxx_fasync,
.release = xxx_release,
......
};
// 当设备可以访问的时候,驱动程序需要向应用程序发出信号,相当于产生“中断”。 kill_fasync函数负责发送指定的信号,
/*
*@fp:要操作的 fasync_struct。
*@sig 要发送的信号。
*@band 可读时设置为 POLL_IN,可写时设置为 POLL_OUT。
*/
void kill_fasync(struct fasync_struct **fp, int sig, int band)
// 例如
kill_fasync(&xxx_dev->async_queue, SIGIO, POLL_IN);
// 这个时候,应用程序注册的信号的服务函数就会被调用