字符设备驱动 I/O模型(实现异步通知)示例

这篇文章展示了如何在Linux内核中编写一个简单的字符设备驱动,包括初始化cdev结构、注册设备号、实现open、read、write等方法。特别是,它详细解释了fasync机制,用于在设备数据可用时向应用程序发送SIGIO信号,实现了异步通知功能。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/poll.h>

#define CNAME "mycdev"
struct cdev cdev;
int major = 0;
int minor = 0;
const int count = 3;
struct class *cls;
struct device *dev;
char kbuf[128] = {0};

//一:定义一个fasync_struct结构体指针变量
struct fasync_struct *fapp;

int mycdev_open(struct inode *inode, struct file *file){
    
    printk("%s:%s(%d)\n",__FILE__,__func__,__LINE__);
    return 0;
}

ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *offs){
    int ret = 0;
    printk("%s:%s(%d)\n",__FILE__,__func__,__LINE__);

    //从硬件中读取数据
    //将数据copy到用户空间
    //校验大小
    if(size > sizeof(kbuf)){
        size = sizeof(kbuf);
    }
    //数据传输
    ret = copy_to_user(ubuf, kbuf, size);
    if(ret){
        printk("copy_to_user error\n");
        return -EIO;
    }

    return size;
}

ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *offs){
    int ret;
    printk("%s:%s(%d)\n",__FILE__,__func__,__LINE__);
    //校验大小
    if(size > sizeof(kbuf)) size = sizeof(kbuf);
    //数据传输
    ret = copy_from_user(kbuf,ubuf,size);
    if(ret){
        printk("copy_from_user error\n");
        return -EIO;
    }

    //三:发信号
    //extern void kill_fasync(struct fasync_struct **, int 信号号, int 信号类型);
    kill_fasync(&fapp, SIGIO, POLL_IN);

    return size;
}

int mycdev_fasync(int fd, struct file *file, int on){
    //几乎所有字符设备驱动程序都使用Fasync_helper()来设置fasync队列,
    //对于常规文件,则使用文件租约代码。
    //错误时返回负,如果没有做任何更改则返回0,如果添加/删除条目则返回正。
    return fasync_helper(fd, file, on, &fapp);
}

int mycdev_close(struct inode*inode, struct file*file){
    printk("%s:%s(%d)\n",__FILE__,__func__,__LINE__);
    return 0;
}

const struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,

    //二:增添fasync操作,异步通知底层函数,完成下一行的工作
    //应用层注册的signal(SIGIO,信号处理函数) 发信号前的fasync_struct结构体的初始化工作
    //应用层将本程序的进程号告诉内核 fcntl(fd, F_SETOWN, getpid());
    //应用层通过 fcntl 调用到底层fasync 操作如下:
    //  unsigned int flags = fcntl(fd, F_GETFL);
    //  fcntl(fd, F_SETFL, flags|FASYNC);
    //即改变fasync标记的时候,驱动中的fasync函数会被调用
    .fasync = mycdev_fasync,

    .release = mycdev_close,
};

static int __init mycdev_init(void){
    int ret = 0;
    dev_t devno = 0;
    int i = 0;

    //1.初始化cdev
    cdev_init(&cdev,&fops);
    cdev.owner = THIS_MODULE;

    //2.申请设备号
    if(major > 0){
        ret = register_chrdev_region(MKDEV(major,minor),count,CNAME);
        if(ret){
            printk("register_chrdev_region error\n");
            goto ERR1;
        }
    }else if(major == 0){
        ret = alloc_chrdev_region(&devno,minor,count,CNAME);
        if(ret){
            printk("alloc_chrdev_region error\n");
            goto ERR1;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
    }

    //3.注册
    ret = cdev_add(&cdev,MKDEV(major,minor),count);
    if(ret){
        printk("cdev_add error\n");
        goto ERR2;
    }

    //4.创建设备节点
    cls = class_create(THIS_MODULE,CNAME);
    if(IS_ERR(cls)){
        printk("class_creat error\n");
        goto ERR3;
    }
    for(i = 0; i < count; i++){
        dev = device_create(cls, NULL, MKDEV(major,i), NULL, "mycdev%d",i);
        if(IS_ERR(dev)){
            printk("device_creat error\n");
            goto ERR4;
        }
    }

    return 0;

ERR4:
    for(--i; i >= 0; i--){
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);
ERR3:
    cdev_del(&cdev);
ERR2:
    unregister_chrdev_region(MKDEV(major,minor),count);
ERR1:
    return ret;
}

static void __exit mycdev_exit(void){
    int i = 0;
    for(i = 0; i < count; i++){
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);
    cdev_del(&cdev);
    unregister_chrdev_region(MKDEV(major, minor), count);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

test.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>

int fd;
char buf[128] = { 0 };
void signal_handle(int signo)
{
    if (signo == SIGIO) {
        memset(buf,0,sizeof(buf));
        read(fd, buf, sizeof(buf));
        printf("buf = %s\n",buf);
    }
}
int main(int argc, const char* argv[])
{

    if ((fd = open("/dev/mycdev0", O_RDWR)) == -1) {
        perror("open mycdev error");
        exit(-1);
    }

    if (signal(SIGIO, signal_handle) == SIG_ERR)
        perror("signal error");
        exit(-1);

    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | FASYNC);

    fcntl(fd, F_SETOWN, getpid());

    while (1) {
        sleep(1);
    }

    close(fd);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值