一、linux信号驱动IO介绍
信号驱动 IO 不需要应用程序查询设备的状态,一旦设备准备就绪,会触发SIGIO信号,进而调用注册的处理函数。
1.1、linux信号驱动IO应用层
如果要实现信号驱动 IO,需要应用程序和驱动程序配合,应用程序使用信号驱动IO的步骤有三步:
步骤 1 :注册信号处理函数 应用程序使用 signal 函数来注册SIGIO 信号的信号处理函数。
步骤 2: 设置能够接收这个信号的进程
步骤 3: 开启信号驱动 IO 通常使用 fcntl 函数的 F_SETFL 命令打开FASYNC 标志。
fcntl 函数如下所示:
函数原型:
int fcntl(int fd,int cmd, …)
函数功能:
fcntl 函数可以用来操作文件描述符
函数参数:
fd: 被操作的文件描述符
cmd: 操作文件描述符的命令,cmd 参数决定了要如何操作文件描述符fd
…: 根据 cmd 的参数来决定是不是需要使用第三个参数
操作文件描述符的命令如下表
命令名 | 描述 |
---|---|
F_DUPFD | 复制文件描述符 |
F_GETFD | 获取文件描述符标志 |
F_SETFD | 设置文件描述符标志 |
F_GETFL | 获取文件状态标志 |
F_SETFL | 设置文件状态标志 |
F_GETLK | 获取文件锁 |
F_SETLK | 设置文件锁 |
F_SETLKW | 类似 F_SETLK,但等待返回 |
F_GETOWN | 获取当前接收 SIGIO 和SIGURG 信号的进程ID和进程组 ID |
F_SETOWN | 设置当前接收 SIGIO 和SIGURG 信号的进程ID和进程组 ID |
1.2、linux信号驱动IO驱动层
当应用程序开启信号驱动 IO 时,会触发驱动中的 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)
当设备准备好的时候,驱动程序需要调用 kill_fasync 函数通知应用程序,此时应用程序的SIGIO 信号处理函数就会被执行。kill_fasync 负责发送指定的信号,函数原型如下:
void kill_fasync(struct fasync_struct **fp,int sig,int band)
函数参数:
fp: 要操作的 fasync_struct
sig: 发送的信号
band: 可读的时候设置成 POLLIN ,可写的时候设置成POLLOUT
二、代码示例
2.1、应用层程序
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>int fd;
char buf1[32] = {0}; //SIGIO信号的信号处理函数
static void func(int signum)
{read(fd,buf1,32);printf ("buf is %s\n",buf1);
}
int main(int argc, char *argv[])
{int ret;int flags;fd = open("/dev/test", O_RDWR); //打开led驱动if (fd < 0){perror("open error \n");return fd;}signal(SIGIO,func); //步骤一:使用signal函数注册SIGIO信号的信号处理函数//步骤二:设置能接收这个信号的进程//fcntl函数用来操作文件描述符,//F_SETOWN 设置当前接收的SIGIO的进程IDfcntl(fd,F_SETOWN,getpid()); flags = fcntl(fd,F_GETFD); //获取文件描述符标志//步骤三 开启信号驱动IO 使用fcntl函数的F_SETFL命令打开FASYNC标志fcntl(fd,F_SETFL,flags| FASYNC);while(1);close(fd); //关闭文件return 0;
}
2.2、驱动层程序
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fcntl.h>
#include <linux/signal.h>struct device_test{dev_t dev_num; //设备号int major ; //主设备号int minor ; //次设备号struct cdev cdev_test; // cdevstruct class *class; //类struct device *device; //设备char kbuf[32];int flag; //标志位struct fasync_struct *fasync;
};struct device_test dev1; DECLARE_WAIT_QUEUE_HEAD(read_wq); //定义并初始化等待队列头/*打开设备函数*/
static int cdev_test_open(struct inode *inode, struct file *file)
{file->private_data=&dev1;//设置私有数据return 0;
}/*向设备写入数据函数*/
static ssize_t cdev_test_write(struct file *file, const char __user *buf, size_t size, loff_t *off)
{struct device_test *test_dev=(struct device_test *)file->private_data;if (copy_from_user(test_dev->kbuf, buf, size) != 0) // copy_from_user:用户空间向内核空间传数据{printk("copy_from_user error\r\n");return -1;}test_dev->flag=1;wake_up_interruptible(&read_wq);kill_fasync(&test_dev->fasync,SIGIO,POLLIN);return 0;
}/**从设备读取数据*/
static ssize_t cdev_test_read(struct file *file, char __user *buf, size_t size, loff_t *off)
{struct device_test *test_dev=(struct device_test *)file->private_data;if(file->f_flags & O_NONBLOCK ){if (test_dev->flag !=1)return -EAGAIN;}wait_event_interruptible(read_wq,test_dev->flag);if (copy_to_user(buf, test_dev->kbuf, strlen( test_dev->kbuf)) != 0) // copy_to_user:内核空间向用户空间传数据{printk("copy_to_user error\r\n");return -1;}return 0;
}static int cdev_test_release(struct inode *inode, struct file *file)
{return 0;
}static __poll_t cdev_test_poll(struct file *file, struct poll_table_struct *p){struct device_test *test_dev=(struct device_test *)file->private_data; //设置私有数据__poll_t mask=0; poll_wait(file,&read_wq,p); //应用阻塞if (test_dev->flag == 1) {mask |= POLLIN;}return mask;}static int cdev_test_fasync (int fd, struct file *file, int on)
{struct device_test *test_dev=(struct device_test *)file->private_data; //设置私有数据return fasync_helper(fd,file,on,&test_dev->fasync);
}
/*设备操作函数*/
struct file_operations cdev_test_fops = {.owner = THIS_MODULE, //将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块.open = cdev_test_open, //将open字段指向chrdev_open(...)函数.read = cdev_test_read, //将open字段指向chrdev_read(...)函数.write = cdev_test_write, //将open字段指向chrdev_write(...)函数.release = cdev_test_release, //将open字段指向chrdev_release(...)函数.poll = cdev_test_poll, //将poll字段指向chrdev_poll(...)函数.fasync = cdev_test_fasync, //将fasync字段指向cdev_test_fasync(...)函数
};static int __init chr_fops_init(void) //驱动入口函数
{/*注册字符设备驱动*/int ret;/*1 创建设备号*/ret = alloc_chrdev_region(&dev1.dev_num, 0, 1, "alloc_name"); //动态分配设备号if (ret < 0){goto err_chrdev;}printk("alloc_chrdev_region is ok\n");dev1.major = MAJOR(dev1.dev_num); //获取主设备号dev1.minor = MINOR(dev1.dev_num); //获取次设备号printk("major is %d \r\n", dev1.major); //打印主设备号printk("minor is %d \r\n", dev1.minor); //打印次设备号/*2 初始化cdev*/dev1.cdev_test.owner = THIS_MODULE;cdev_init(&dev1.cdev_test, &cdev_test_fops);/*3 添加一个cdev,完成字符设备注册到内核*/ret = cdev_add(&dev1.cdev_test, dev1.dev_num, 1);if(ret<0){goto err_chr_add;}/*4 创建类*/dev1. class = class_create(THIS_MODULE, "test");if(IS_ERR(dev1.class)){ret=PTR_ERR(dev1.class);goto err_class_create;}/*5 创建设备*/dev1.device = device_create(dev1.class, NULL, dev1.dev_num, NULL, "test");if(IS_ERR(dev1.device)){ret=PTR_ERR(dev1.device);goto err_device_create;}return 0;err_device_create:class_destroy(dev1.class); //删除类err_class_create:cdev_del(&dev1.cdev_test); //删除cdeverr_chr_add:unregister_chrdev_region(dev1.dev_num, 1); //注销设备号err_chrdev:return ret;
}static void __exit chr_fops_exit(void) //驱动出口函数
{/*注销字符设备*/unregister_chrdev_region(dev1.dev_num, 1); //注销设备号cdev_del(&dev1.cdev_test); //删除cdevdevice_destroy(dev1.class, dev1.dev_num); //删除设备class_destroy(dev1.class); //删除类
}
module_init(chr_fops_init);
module_exit(chr_fops_exit);
2.3、linux信号驱动IO使用API要点
应用层
signal(SIGIO,func); //步骤一:使用signal函数注册SIGIO信号的信号处理函数//步骤二:设置能接收这个信号的进程//fcntl函数用来操作文件描述符,//F_SETOWN 设置当前接收的SIGIO的进程IDfcntl(fd,F_SETOWN,getpid()); flags = fcntl(fd,F_GETFD); //获取文件描述符标志//步骤三 开启信号驱动IO 使用fcntl函数的F_SETFL命令打开FASYNC标志fcntl(fd,F_SETFL,flags| FASYNC);
驱动层
kill_fasync(&test_dev->fasync,SIGIO,POLLIN);
static int cdev_test_fasync (int fd, struct file *file, int on)
{struct device_test *test_dev=(struct device_test *)file->private_data; //设置私有数据return fasync_helper(fd,file,on,&test_dev->fasync);
}