2.8字符设备驱动之poll

多路复用简介

多路复用即同时等待多个IO,当某些IO发生相应的事件后则对其进行对应的操作。

应用层多路复用

在应用层可以通过poll、select、epoll来实现多路复用,在使用poll、select、epoll进行多路复用时强烈建议采用非阻塞方式打开相应的设备或文件,以免错误操作导致IO卡死

驱动层的多路复用框架

要使驱动支持多路复用只需要在驱动层实现对应的poll函数即可,如下是驱动中poll函数的固定框架:

unsigned int xxx_poll(struct file *filp, struct poll_table_struct *p)
{
	unsigned int mask = 0;
	
	//调用poll_wait,将进程加入到可能会改变进程状态的等待队列中,并将等待队列的信息添加到以poll_table扩
	//展的数据结构中,此函数不会引起进程休眠
	poll_wait(filp, &wq, p);
	poll_wait(filp, &rq, p);
	......
	
	//如果发生读事件
	//POLLIN 通常等价 POLLRDNORM ,表示发生读事件,即驱动有数据可读
	if(...)
		mask |= POLLIN | POLLRDNORM;
	
	//如果发生写事件
	//POLLOUT 通常等价 POLLWRNORM ,表示发生写事件,即驱动可以接受应用层数据
	if(...)
		mask |= POLLOUT | POLLWRNORM;
	
	//返回相应的事件,没有发生任何事件则返回0
	return mask;
}

驱动代码实现

驱动代码在上一章节的基础上进行修改,主要增加了poll函数,剩余部分基本相似,仅仅是修改了函数名而已,如下是这驱动程序的代码:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#include <linux/kfifo.h>
#include <linux/poll.h>

//次设备号,为MISC_DYNAMIC_MINOR表示自动分配
#define POLL_MINOR	MISC_DYNAMIC_MINOR
//设备文件名
#define POLL_NAME	"poll"


DEFINE_KFIFO(gfifo, char, 32);
//定义并初始化等待队列
DECLARE_WAIT_QUEUE_HEAD(read_queue);
DECLARE_WAIT_QUEUE_HEAD(write_queue);


//打开设备,对应应用层中的open
static int poll_open(struct inode *inode, struct file *file)
{
	return 0;
}

//释放设备,在应用层最后因此调用close时调用
static int poll_release(struct inode *inode, struct file *file)
{
	return 0;
}

//读数据,对应应用层中的read
static ssize_t poll_read(struct file *file, char __user *buf, size_t len, loff_t *pos)
{
	unsigned int copied;

	if(kfifo_is_empty(&gfifo))
	{
//		printk("fifo is empty\r\n");
		//以非阻塞式打开,直接返回-EAGAIN
		if(file->f_flags & O_NONBLOCK)
			return -EAGAIN;

		//挂起当前进程到等待队列,能被信号唤醒
		if(wait_event_interruptible_exclusive(read_queue, !kfifo_is_empty(&gfifo)))
		{
			//返回-ERESTARTSYS,表示被信号唤醒
			return -ERESTARTSYS;
		}
	}

	//将FIFO中的数据读取到应用层
	if(kfifo_to_user(&gfifo, buf, len, &copied) != 0)
	{
		printk("Bad address\r\n");
		return -EFAULT;
	}

	//如果可以写入则唤醒写进程
	if(!kfifo_is_full(&gfifo))
		wake_up_interruptible(&write_queue);

	return copied;
}

//写设备,对应应用层中的write
static ssize_t poll_write(struct file *file, const char __user *buf, size_t len, loff_t *pos)
{
	unsigned int copied;

	if(kfifo_is_full(&gfifo))
	{
//		printk("fifo is full\r\n");
		//以非阻塞式打开,直接返回-EAGAIN
		if(file->f_flags & O_NONBLOCK)
			return -EAGAIN;

		//挂起当前进程到等待队列,能被信号唤醒
		if(wait_event_interruptible_exclusive(write_queue, !kfifo_is_full(&gfifo)))
		{
			//返回-ERESTARTSYS,表示被信号唤醒
			return -ERESTARTSYS;
		}
	}

	//将应用层的数据写入FIFO
	if(kfifo_from_user(&gfifo, buf, len, &copied) != 0)
	{
		printk("Bad address\r\n");
		return -EFAULT;
	}

	//如果有数据可读则唤醒读进程
	if(!kfifo_is_empty(&gfifo))
		wake_up_interruptible(&read_queue);

	return copied;
}

static unsigned int poll_poll(struct file *filp, struct poll_table_struct *p)
{
	unsigned int mask = 0;

	//调用poll_wait,将进程加入到可能会改变进程状态的等待队列中,并将等待队列的添加到以poll_table扩展的数据结构中,此函数不会引起进程休眠
	poll_wait(filp, &read_queue, p);
	poll_wait(filp, &write_queue, p);

	//如果发生读事件
	//POLLIN 通常等价 POLLRDNORM ,表示发生读事件,即驱动有数据可读
	if(!kfifo_is_empty(&gfifo))
	{
//		printk("read");
		mask |= POLLIN | POLLRDNORM;
	}

	//如果发生写事件
	//POLLOUT 通常等价 POLLWRNORM ,表示发生写事件,即驱动可以接收应用层数据
	if(!kfifo_is_full(&gfifo))
	{
//		printk("write");
		mask |= POLLOUT | POLLWRNORM;
	}

	//返回相应的事件,没有发生任何事件则返回0
	return mask;
}

static struct file_operations ops = {
	.owner = THIS_MODULE,
	.write = poll_write,
	.read = poll_read,
	.poll = poll_poll,
	.open = poll_open,
	.release = poll_release,
};

static struct miscdevice poll_misc = {
	.name = POLL_NAME,
	.minor = POLL_MINOR,
	.fops = &ops,
};
static int __init poll_init(void)
{
	int err = 0;

	printk("poll fifo init\r\n");
	//注册混杂设备
	err = misc_register(&poll_misc);
	if(err != 0)
	{
		printk("register misc failed\r\n");
		return err;
	}
	return 0;
}

static void __exit poll_exit(void)
{
	printk("poll fifo exit\r\n");
	//注销混杂设备
	misc_deregister(&poll_misc);
}

module_init(poll_init);
module_exit(poll_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("csdn");
MODULE_DESCRIPTION("poll");

驱动测试程序实现

驱动测试程序使用函数来监测打开的设备文件描述符的状态,当发生POLLIN事件时调用read函数,读取驱动数据并提供printf输出,当发生POLLOUT事件是调用write函数将数据写入驱动,在程序开头还有一段判断工作模式的代码,工作在read状态关注POLLIN事件,否则关注POLLOUT事件,如下是整个驱动测试程序的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>

char rbuf[100] = {0};
char *wstr = "123456789123456789\r\n";

int main(int argc, char *argv[])
{
	int fd;
	int i;
	int ret;
	int is_read;
	int write_index;
	int str_lenght;
	struct pollfd  fds[1];

	//确定工作模式,读或者写
	if((argc >= 2) && (strncmp(argv[1], "read", 4) == 0))
		is_read = 1;
	else
		is_read = 0;

	write_index = 0;
	str_lenght = strlen(wstr);

	//打开设备,使用select、poll、epoll轮询设备状态时建议采用非阻塞方式打开设备
	fd = open("/dev/poll", O_RDWR|O_NONBLOCK);
	if(fd == -1)
	{
		perror("error");
		return -1;
	}

	//将设备描述符加入pollfd列表,对于读操作只关心POLLIN事件,对于写操作只关心POLLOUT
	fds[0].fd = fd;
	if(is_read == 1)
		fds[0].events = POLLIN;
	else
		fds[0].events = POLLOUT;
	fds[0].revents = 0;

	while(1)
	{
		//执行poll,轮询pollfd列表中的描述符,直到发生相应事件或者超时
		ret = poll(fds, 1, -1);
		if(ret == -1)
			break;

		//判断是否发生读事件,发生的事件存储在revents中
		if(fds[0].revents & POLLIN)
		{
			//读数据,并通过printf输出
			memset(rbuf, 0, 100);
			ret = read(fd, rbuf, 99);
			if(ret <= 0)
				break;
			printf("%s", rbuf);
			fflush(stdout);
		}

		//判断是否发生写事件
		if(fds[0].revents & POLLOUT)
		{
			//写数据
			ret = write(fd, &wstr[write_index], str_lenght-write_index);
			if(ret <= 0)
				break;
			write_index += ret;
			if(write_index >= str_lenght)
				write_index = 0;
		}
	}

	//关闭设备
	close(fd);

	return 0;
}

上机实验

  1. 这里下载代码并进行编译,然后拷贝到目标板/root目录
  2. 执行insmod poll.ko加载驱动
  3. 执行./app.out read启动测试程序,此时进行的read操作
  4. 在开一个终端执行./app.out启动测试程序,此时进行的是write操作
  5. 此时可看到执行./app.out read的终端在不停的输出读取到的数据
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值