轮询操作
关于用户空间的轮询操作,poll,select,epoll等自行查询,这里只简单介绍设备驱动中的poll函数
poll函数一个典型模板:
unsigned int poll_chr_poll_func(struct file* filp, poll_table* wait)
{
unsigned int mask = 0;
// 获取设备结构体指针
struct xxx_dev* dev = filp->private_data;
...
// 分别加入读写等待队列
poll_wait(filp, &dev->r_wq, wait);
poll_wait(filp, &dev->w_wq, wait);
// 可读
if (...)
mask |= POLLIN | POLLRDNORM;
// 可写
if (...)
mask |= POLLOUT | POLLWRNORM;
return mask;
}
这个函数主要进行下面两项工作
- 对可能引起设备文件状态变化的等待队列调用
poll_wair()
函数,将对应的等待队列头添加到poll_table
结构体中 - 返回表示是否能对设备进行无阻赛的读、写访问的掩码
代码
/*
* @Date: 2024-05-07 15:37:32
* @author: lidonghang-02 2426971102@qq.com
* @LastEditTime: 2024-05-19 19:20:51
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include "poll_chr.h"
#define POLL_CHR_SIZE 0x10
#define POLL_CHR_MAJOR 0
#define POLL_CHR_MINOR 0
#define POLL_CHR_NR_DEVS 1
static int poll_chr_major = POLL_CHR_MAJOR;
static int poll_chr_minor = POLL_CHR_MINOR;
static int poll_chr_nr_devs = POLL_CHR_NR_DEVS;
static struct class* poll_chr_cls;
struct poll_chr_dev
{
struct cdev cdev;
struct device* class_dev;
unsigned int len;
unsigned char mem[POLL_CHR_SIZE];
struct mutex mutex;
wait_queue_head_t r_wq;
wait_queue_head_t w_wq;
};
static struct poll_chr_dev* poll_chr_devp;
static int poll_chr_open_func(struct inode* inode, struct file* filp)
{
struct poll_chr_dev* dev = container_of(inode->i_cdev, struct poll_chr_dev, cdev);
filp->private_data = dev;
printk(KERN_INFO "poll_chr_open\n");
return 0;
}
static int poll_chr_release_func(struct inode* inode, struct file* filp)
{
printk(KERN_INFO "poll_chr_release\n");
return 0;
}
static ssize_t poll_chr_read_func(struct file* filp, char __user* buf, size_t count, loff_t* f_pos)
{
struct poll_chr_dev* dev = filp->private_data;
int ret = 0;
DECLARE_WAITQUEUE(wait, current);
mutex_lock(&dev->mutex);
add_wait_queue(&dev->r_wq, &wait);
while (dev->len == 0)
{
if (filp->f_flags & O_NONBLOCK)
{
ret = -EAGAIN;
goto out_1;
}
__set_current_state(TASK_INTERRUPTIBLE);
mutex_unlock(&dev->mutex);
schedule();
if (signal_pending(current))
{
ret = -ERESTARTSYS;
goto out_2;
}
mutex_lock(&dev->mutex);
}
if (count > dev->len)
count = dev->len;
if (copy_to_user(buf, dev->mem, count) != 0)
ret = -EFAULT;
else
{
dev->len = dev->len - count;
memcpy(dev->mem, dev->mem + count, dev->len);
ret = count;
wake_up_interruptible(&dev->w_wq);
}
out_1:
mutex_unlock(&dev->mutex);
out_2:
remove_wait_queue(&dev->r_wq, &wait);
set_current_state(TASK_RUNNING);
return ret;
}
static ssize_t poll_chr_write_func(struct file* filp, const char __user* buf, size_t count, loff_t* f_pos)
{
int ret = -ENOMEM;
struct poll_chr_dev* dev = filp->private_data;
DECLARE_WAITQUEUE(wait, current);
mutex_lock(&dev->mutex);
add_wait_queue(&dev->w_wq, &wait);
while (dev->len == POLL_CHR_SIZE)
{
if (filp->f_flags & O_NONBLOCK)
{
ret = -EAGAIN;
goto out_1;
}
__set_current_state(TASK_INTERRUPTIBLE);
mutex_unlock(&dev->mutex);
schedule();
if (signal_pending(current))
{
ret = -ERESTARTSYS;
goto out_2;
}
mutex_lock(&dev->mutex);
}
if (count > POLL_CHR_SIZE - dev->len)
count = POLL_CHR_SIZE - dev->len;
if (copy_from_user(dev->mem + dev->len, buf, count))
ret = -EFAULT;
else
{
dev->len += count;
wake_up_interruptible(&dev->r_wq);
ret = count;
}
out_1:
mutex_unlock(&dev->mutex);
out_2:
remove_wait_queue(&dev->w_wq, &wait);
set_current_state(TASK_RUNNING);
return ret;
}
static long poll_chr_ioctl_func(struct file* filp, unsigned int cmd, unsigned long arg)
{
struct poll_chr_dev* dev = filp->private_data;
int ret = 0;
// 检查幻数(返回值POSIX标准规定,也用-EINVAL)
if (_IOC_TYPE(cmd) != POLL_CHR_MAGIC)
return -ENOTTY;
// 检查命令编号
if (_IOC_NR(cmd) > POLL_CHR_MAXNR)
return -ENOTTY;
// 检查命令方向,并验证用户空间指针的访问权限。
if (_IOC_DIR(cmd) & _IOC_READ)
ret = !access_ok(VERIFY_WRITE, (void __user*)arg, _IOC_SIZE(cmd));
else if (_IOC_DIR(cmd) & _IOC_WRITE)
ret = !access_ok(VERIFY_READ, (void __user*)arg, _IOC_SIZE(cmd));
if (ret)
return -EFAULT;
switch (cmd)
{
case POLL_CHR_CLEAR:
mutex_lock(&dev->mutex);
dev->len = 0;
memset(dev->mem, 0, POLL_CHR_SIZE);
mutex_unlock(&dev->mutex);
break;
default:
return -EINVAL;
}
return 0;
}
static unsigned int poll_chr_poll_func(struct file* filp, poll_table* wait)
{
unsigned int mask = 0;
struct poll_chr_dev* dev = filp->private_data;
mutex_lock(&dev->mutex);
poll_wait(filp, &dev->r_wq, wait);
poll_wait(filp, &dev->w_wq, wait);
if (dev->len != 0)
mask |= POLLIN | POLLRDNORM;
if (dev->len != POLL_CHR_SIZE)
mask |= POLLOUT | POLLWRNORM;
mutex_unlock(&dev->mutex);
return mask;
}
static const struct file_operations poll_chr_fops =
{
.owner = THIS_MODULE,
.read = poll_chr_read_func,
.write = poll_chr_write_func,
.unlocked_ioctl = poll_chr_ioctl_func,
.poll = poll_chr_poll_func,
.open = poll_chr_open_func,
.release = poll_chr_release_func,
};
static int __init poll_chr_init_module(void)
{
int ret = 0, i;
dev_t devno = MKDEV(poll_chr_major, poll_chr_minor);
if (poll_chr_major)
ret = register_chrdev_region(devno, poll_chr_nr_devs, "poll_chr");
else
{
ret = alloc_chrdev_region(&devno, poll_chr_minor, poll_chr_nr_devs, "poll_chr");
poll_chr_major = MAJOR(devno);
}
if (ret < 0)
return ret;
poll_chr_devp = kzalloc(sizeof(struct poll_chr_dev) * poll_chr_nr_devs, GFP_KERNEL);
if (!poll_chr_devp)
{
printk(KERN_WARNING "alloc mem failed");
ret = -ENOMEM;
goto out_1;
}
poll_chr_cls = class_create(THIS_MODULE, "poll_chr");
if (IS_ERR(poll_chr_cls))
{
printk(KERN_WARNING "Error creating class for ioctl");
goto out_2;
}
for (i = 0; i < poll_chr_nr_devs; i++)
{
cdev_init(&poll_chr_devp[i].cdev, &poll_chr_fops);
poll_chr_devp[i].cdev.owner = THIS_MODULE;
ret = cdev_add(&poll_chr_devp[i].cdev, MKDEV(poll_chr_major, poll_chr_minor + i), 1);
if (ret)
printk(KERN_WARNING "Error adding cdev for device %d", i);
else
{
poll_chr_devp[i].class_dev = device_create(poll_chr_cls, NULL, MKDEV(poll_chr_major, poll_chr_minor + i), NULL, "poll_chr%d", i);
if (IS_ERR(poll_chr_devp[i].class_dev))
printk(KERN_WARNING "Error creating device for device %d", i);
}
mutex_init(&poll_chr_devp[i].mutex);
init_waitqueue_head(&poll_chr_devp[i].r_wq);
init_waitqueue_head(&poll_chr_devp[i].w_wq);
}
return 0;
out_2:
kfree(poll_chr_devp);
out_1:
unregister_chrdev_region(devno, poll_chr_nr_devs);
return ret;
}
static void __exit poll_chr_exit_module(void)
{
int i;
for (i = 0; i < poll_chr_nr_devs; i++)
{
device_destroy(poll_chr_cls, MKDEV(poll_chr_major, poll_chr_minor + i));
cdev_del(&poll_chr_devp[i].cdev);
}
class_destroy(poll_chr_cls);
kfree(poll_chr_devp);
unregister_chrdev_region(MKDEV(poll_chr_major, poll_chr_minor), poll_chr_nr_devs);
printk(KERN_INFO "poll_chr exit\n");
}
module_param(poll_chr_major, int, S_IRUGO);
module_param(poll_chr_minor, int, S_IRUGO);
module_param(poll_chr_nr_devs, int, S_IRUGO);
module_init(poll_chr_init_module);
module_exit(poll_chr_exit_module);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lidonghang-02");
#ifndef _POLL_CHR_H_
#define _POLL_CHR_H_
#define POLL_CHR_MAGIC 'c'
#define POLL_CHR_CLEAR _IO(POLL_CHR_MAGIC, 0)
#define POLL_CHR_MAXNR 1
#endif /* _POLL_CHR_H_ */
测试
epoll
/*
* @Date: 2024-05-12 18:13:03
* @author: lidonghang-02 2426971102@qq.com
* @LastEditTime: 2024-05-19 19:18:37
*/
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <strings.h>
#include <sys/epoll.h>
#include "poll_chr.h"
int main()
{
int fd = open("/dev/poll_chr0", O_RDONLY | O_NONBLOCK);
if (fd != -1)
{
struct epoll_event ev_poll_chr;
int err;
int epfd;
if (ioctl(fd, POLL_CHR_CLEAR, 0) < 0)
{
printf("ioctl error\n");
return 0;
}
epfd = epoll_create(1);
if (epfd < 0)
{
perror("epoll_create()");
return;
}
bzero(&ev_poll_chr, sizeof(struct epoll_event));
ev_poll_chr.events = EPOLLIN | EPOLLPRI;
err = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev_poll_chr);
if (err < 0)
{
perror("epoll_ctl1 error");
return 0;
}
err = epoll_wait(epfd, &ev_poll_chr, 1, 10000);
if (err < 0)
perror("epoll_wait error");
else if (err == 0)
printf("timeout\n");
else
printf("not empty\n");
err = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev_poll_chr);
if (err < 0)
perror("epoll_ctl2 error");
}
else
printf("open error\n");
return 0;
}
select
/*
* @Date: 2024-05-12 17:31:54
* @author: lidonghang-02 2426971102@qq.com
* @LastEditTime: 2024-05-19 19:18:40
*/
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include "poll_chr.h"
int main()
{
fd_set rfds, wfds; /* 读/写文件描述符集 */
int fd = open("/dev/poll_chr0", O_RDONLY | O_NONBLOCK);
if (fd != -1)
{
if (ioctl(fd, POLL_CHR_CLEAR, 0) < 0)
printf("ioctl error\n");
else
{
while (1)
{
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_SET(fd, &rfds);
FD_SET(fd, &wfds);
select(fd + 1, &rfds, &wfds, NULL, NULL);
/* 数据可获得 */
if (FD_ISSET(fd, &rfds))
printf("Poll monitor:can be read\n");
/* 数据可写入 */
if (FD_ISSET(fd, &wfds))
printf("Poll monitor:can be written\n");
sleep(1);
}
}
}
else
printf("open error\n");
return 0;
}