阻塞 | 非阻塞 | |
---|---|---|
同步 | 阻塞IO | 非阻塞IO |
异步 | IO多路复用 | 异步IO |
?内核将对设备控制的操作提供了ioctl接口。
int ioctl(int d, int request, ……);
//d:表示要操作的文件描述符
//request:代表不同操作的数字值
?看前面file_operations结构的定义,和ioctl对应的驱动函数的接口是unlocked_ioctl和compat_ioctl(32位64位兼容用,不讨论)。
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
//分别和ioctl的3个参数对应
?在内核文档Documentation/ioctl/ioctl-decoding.txt中定义了ioctl request参数需要遵守的编码规则,这样的目的是为了避免命令的重复,从而导致应用程序的误操作。底层宏_IOC将几个部分通过移位合并。
?通过源码fs/ioctl.c可知unlocked_ioctl接口实现就是一个大的switch语句。
/*vser.h*/
#ifndef _VSER_H
#define _VSER_H
struct option {
unsigned int datab; //波特率
unsigned int parity; //奇偶校验
unsigned int stopb; //停止位
};
#define VS_MAGIC ‘s‘
#define VS_SET_BAUD _IOW(VS_MAGIC, 0, unsigned int) //设置波特率
#define VS_GET_BAUD _IOW(VS_MAGIC, 1, unsigned int) //获取波特率
#define VS_SET_FFMT _IOW(VS_MAGIC, 2, struct option) //设置帧格式
#define VS_GET_FFMT _IOW(VS_MAGIC, 3, struct option) //获得帧格式
#endif
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kfifo.h>
#include <linux/ioctl.h>
#include <linux/uaccess.h>
#include "vser.h"
#define VSER_MAJOR 256
#define VSER_MINOR 0
#define VSER_DEV_CNT 1
#define VSER_DEV_NAME "vser"
struct vser_dev{
unsigned int baud;
struct option opt;
struct cdev cdev;
}
DEFINE_KFIFO(vsfifo, char, 32); //定义并初始化FIFO对象
static struct vser_dev vsdev;
static int vser_open(struct inode *inode, struct file *filp){
return 0;
}
static int vser_release(struct inode *inode, struct file *filp){
return 0;
}
static ssize_t vser_read(struct file *filp, char __user *buf, size_t count, loff_t *pos) {
int ret;
unsigned int copied = 0;
ret = kfifo_to_user(&vsfifo, buf, count, &copied); //将FIFO数据取出,复制到用户空间(内核视角)
return ret == 0? copied: ret;
}
static ssize_t vser_write(struct file *filp, const char __user *buf, size_t count, loff_t *pos) {
int ret;
unsigned int copied = 0;
ret = kfifo_from_user(&vsfifo, buf, count, &copied);
return ret == 0? copied: ret;
}
static long vser_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) {
if(_IOC_TYPE(cmd) != VS_MAGIC)
return -ENOTTY;
switch(cmd) {
case VS_SET_BAUD:
vsdev.baud = arg;
break;
case VS_GET_BAUD:
arg = vsdev.baud;
break;
case VS_SET_FFMT:
if(copy_from_user(&vsdev.opt, (struct option __user *)arg, sizeof(struct option)))
return -EFAULT;
break;
case VS_GET_FFMT:
if(copy_to_user((struct option __user *)arg, &vsdev.opt, sizeof(struct option)))
return -EFAULT;
break;
default:
return -ENOTTY;
}
return 0;
}
static struct file_operations vser_ops = { //定义了对字符设备的不同操作
.owner = THIS_MODULE;
.open = vser_open;
.release = vser_release;
.read = vser_read;
.write = vser_write;
.unlocked_ioctl = vser_ioctl;
}
//加载模块
static int __init vser_init(void) {
int ret;
dev_t dev;
dev = MKDEV(VSER_MAJOR, VSER_MINOR); //将主次设备号合并,主设备号占12位,次设备号占20位
ret = register_chrdev_region(dev, VSER_DEV_CNT, VSER_DEV_NAME);
if(ret)
goto reg_err;
cdev_init(&vsdev, &vser_ops);
vsdev.cdev.owner = THIS_MODULE;
vsdev.baud = 115200;
vsdev.opt.datab = 8;
vsdev.opt.parity = 0;
vsdev.opt.stopb = 1;
ret = cdev_add(&vsdev.cdev, dev, VSER_DEV_CNT);
if(ret)
goto add_err;
add_err:
unregister_chrdev_region(dev, VSER_DEV_CNT);
reg_err:
return ret;
}
//卸载模块
static void __exit vser_exit(void) {
printk("vser_exit\n");
dev_t dev;
dev = MKDEV(VSER_MAJOR, VSER_MINOR);
cdev_del(&vsdev);
unregister_chrdev_region(dev, VSER_DEV_CNT);
}
module_init(vser_init);
module_exit(vser_exit);
MODULE_LICENSE("GPL"); //合法协议
?测试程序的完整代码
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include "vser.h"
int main(int argc, char &argv[]) {
int fd, ret;
unsigned int baud;
struct option opt = {8, 1, 1};
fd = fopen("/dev/vser0", O_RDWR);
if(fd == -1)
goto fail;
baud = 9600;
ret = ioctl(fd, VS_SET_BAUD, baud);
if(ret == -1)
goto fail;
ret = ioctl(fd, VS_GET_BAUD, baud);
if(ret == -1)
goto fail;
//类似可以添加另外两个幻数
close(fd);
exit(EXIT_SUCCESS);
fail:
perror("ioctl test");
exit(EXIT_FAILURE);
}
?测试的指令如下:
# mknod /dev/vser0 c 256 0
# make
# make modules_install
# depmod
# modprobe vser
# gcc -o test test.c
# ./test
?设备不一定随时可以给用户提供服务。对上面的串口设备而言:
?如果设备以非阻塞的方式打开设备文件,当资源不可用时立即返回,并用错误码EAGAIN
来通知应用程序此时资源不可用,就是非阻塞IO的用法。改造读写函数:
static ssize_t vser_read(struct file *filp, char __user *buf, size_t count, loff_t *pos) {
int ret;
unsigned int copied = 0;
if(kfifo_is_empty(&vsfifo)) //判断FIFO是否为空
if(filp->f_flags & O_NONBLOCK)
return -EAGAIN;
ret = kfifo_to_user(&vsfifo, buf, count, &copied); //将FIFO数据取出,复制到用户空间(内核视角)
return ret == 0? copied: ret;
}
static ssize_t vser_write(struct file *filp, const char __user *buf, size_t count, loff_t *pos) {
int ret;
unsigned int copied = 0;
if(kfifo_is_full(&vsfifo)) //判断FIFO是否为满
if(filp->f_flags & O_NONBLOCK)
return -EAGAIN;
ret = kfifo_from_user(&vsfifo, buf, count, &copied);
return ret == 0? copied: ret;
}
原文:https://www.cnblogs.com/hansenn/p/12738981.html