异步通知&& 信号处理(正点原子笔记)

阻塞通过等待驱动设备可用,非阻塞通过poll 函数不断轮询,两者都是应用程序主动去查询设备的使用情况。

信号因此产生,信号类似于我们硬件上使用的“中断”,是在软件层次上对中断的一种模拟,驱动可以通过主动向应用程序发送信号的方式来报告自己可以访问了。

常见的信号有

#define SIGKILL 9

#define SIGINT 2 /* 终端中断(Ctrl+C 组合键)

信号处理函数

sighandler_t signal(int signum, sighandler_t handler)
回调
typedef void (*sighandler_t)(int)

驱动中的信号处理

1.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.fasync 函数

如果要使用异步通知,需要在设备驱动中实现 file_operations 操作集中的 fasync 函数

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

async 函数里面一般通过调用 fasync_helper 函数来初始化前面定义的 fasync_struct 结构体指针,fasync_helper 函数原型如下:

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

fasync_helper 函数的前三个参数就是 fasync 函数的那三个参数,第四个参数就是要初始化的 fasync_struct 结构体指针变量。当应用程序通过“fcntl(fd, F_SETFL, flags | FASYNC)”改变fasync 标记的时候,驱动程序 file_operations 操作集中的 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, &dev->async_queue) < 0)
         return -EIO;
     return 0;
  }

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


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

 应用程序对异步通知的处理

1、注册信号处理函数

 signal 函数,前面提到过

2.将本应用程序的进程号告诉给内核

使用 fcntl(fd, F_SETOWN, getpid())将本应用程序的进程号告诉给内核。

3、开启异步通知

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

驱动示例

50 struct imx6uirq_dev{
......
64     struct fasync_struct *async_queue; /* 异步相关结构体 */
65 };
66 
67 struct imx6uirq_dev imx6uirq; /* irq 设备 */
68 
......
84
90 void timer_function(unsigned long arg)
91 {
92     unsigned char value;
93     unsigned char num;
94     struct irq_keydesc *keydesc;
95     struct imx6uirq_dev *dev = (struct imx6uirq_dev *)arg;
...... 
109     if(atomic_read(&dev->releasekey)) { /* 一次完整的按键过程 */
110         if(dev->async_queue)
111             kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
112       }
113
114 #if 0
115 /* 唤醒进程 */
116 if(atomic_read(&dev->releasekey)) { /* 完成一次按键过程 */
117     /* wake_up(&dev->r_wait); */
118     wake_up_interruptible(&dev->r_wait);
119 }
120 #endif
121 }
269 static int imx6uirq_fasync(int fd, struct file *filp, int on)
270 {
271     struct imx6uirq_dev *dev = (struct imx6uirq_dev *)filp->private_data;
272     return fasync_helper(fd, filp, on, &dev->async_queue);
273 }
281 static int imx6uirq_release(struct inode *inode, struct file *filp)
282 {
283     return imx6uirq_fasync(-1, filp, 0);
284 }
285
286 /* 设备操作函数 */
287 static struct file_operations imx6uirq_fops = {
288     .owner = THIS_MODULE,
289     .open = imx6uirq_open,
290     .read = imx6uirq_read,
291     .poll = imx6uirq_poll,
292     .fasync = imx6uirq_fasync,
293     .release = imx6uirq_release,
294 };
295
296 /*
297 * @description : 驱动入口函数
298 * @param : 无
299 * @return : 无
300 */
301 static int __init imx6uirq_init(void)
302 {
......
328 
329 /* 5、始化按键 */
330     atomic_set(&imx6uirq.keyvalue, INVAKEY);
331     atomic_set(&imx6uirq.releasekey, 0);
332     keyio_init();
333     return 0;
334 }

测试APP

static int fd = 0;
static void sigio_signal_func(int signum)
33 {
34     int err = 0;
35     unsigned int keyvalue = 0;
36
37     err = read(fd, &keyvalue, sizeof(keyvalue));
38     if(err < 0) {
39         /* 读取错误 */
40     } else {
41         printf("sigio signal! key value=%d\r\n", keyvalue);
42 }
43 }
51 int main(int argc, char *argv[])
52 {
53     int flags = 0;
54     char *filename;
55
56     if (argc != 2) {
57     printf("Error Usage!\r\n");
58     return -1;
59     }
60
61     filename = argv[1];
62     fd = open(filename, O_RDWR);
63     if (fd < 0) {
64         printf("Can't open file %s\r\n", filename);
65     return -1;
66 }
67
68 /* 设置信号 SIGIO 的处理函数 */
69     signal(SIGIO, sigio_signal_func);
70 
71     fcntl(fd, F_SETOWN, getpid()); /* 将当前进程的进程号告诉给内核 */
72     flags = fcntl(fd, F_GETFD); /* 获取当前的进程状态 */
73     fcntl(fd, F_SETFL, flags | FASYNC);/* 设置进程启用异步通知功能 */ 
74
75     while(1) {
76         sleep(2);
77     }
78
79     close(fd);
80     return 0;
81 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值