【Linux设备驱动】--0x03字符设备模块-完整的错误处理+全局数据结构放在同一结构体内

简介: 项目中经常会将所用到的所有全局数据结构放到同一个结构体内struct simple_dev { dev_t dev_no; struct cdev cdev; struct class *class; struct device *device;};该...

项目中经常会将所用到的所有全局数据结构放到同一个结构体内

struct simple_dev {
    dev_t  dev_no;
    struct cdev   cdev;
    struct class  *class;
    struct device *device;
};

该结构体将所用到的全局数据结构放在一起,这样可以在open的时候,将该指针放到file->private_data = dev;私有data字段内,方便其他系统调用接口使用

这里container_of无法操作内部嵌套的指针类型,所以struct cdev cdev;不能用作指针类型

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/slab.h>   // kfree,kmalloc
#include <linux/cdev.h>   // cdev_xxx
#include <linux/device.h> // device_xxx

struct simple_dev {
    dev_t  dev_no;
    struct cdev   cdev;
    struct class  *class;
    struct device *device;
};

struct simple_dev *g_simple_dev;

static int simple_cdev_open(struct inode *inode, struct file *file)
{
    struct simple_dev *dev;

    dev = container_of(inode->i_cdev, struct simple_dev, cdev);
    file->private_data = dev;

    return 0;
}

static int simple_cdev_release(struct inode *inode, struct file *file)
{
    struct simple_dev *dev;

    dev = file->private_data;

    return 0;
}

/* File operations struct for character device */
static const struct file_operations g_simple_dev_fops = {
    .owner   = THIS_MODULE,
    .open    = simple_cdev_open,
    .release = simple_cdev_release,
};

static int __init simple_cdev_init(void)
{
    int ret;

    g_simple_dev = kzalloc(sizeof(struct simple_dev), GFP_KERNEL);
    if (!g_simple_dev) {
        return -1;
    }

    ret = alloc_chrdev_region(&g_simple_dev->dev_no, 0, 1, "simple_cdev");
    if (!ret) {
        goto alloc_chrdev_failed;
    }

    cdev_init(&g_simple_dev->cdev, &g_simple_dev_fops);
    g_simple_dev->cdev.owner = THIS_MODULE;

    ret = cdev_add(&g_simple_dev->cdev, g_simple_dev->dev_no, 1);
    if (!ret) {
        goto cdev_add_failed;
    }

    g_simple_dev->class = class_create(THIS_MODULE, "simple_cdev");
    if (!g_simple_dev->class) {
        goto class_create_failed;
    }

    g_simple_dev->device = device_create(g_simple_dev->class, NULL, g_simple_dev->dev_no, NULL, "simple_cdev");
    if (!g_simple_dev->device) {
        goto device_create_failed;
    }
    
    return 0;

device_create_failed:
    class_destroy(g_simple_dev->class);
class_create_failed:
    cdev_del(&g_simple_dev->cdev);
cdev_add_failed:
    unregister_chrdev_region(g_simple_dev->dev_no, 1);
alloc_chrdev_failed:
    kfree(g_simple_dev);

    return -1;
}

static void __exit simple_cdev_exit(void)
{
    device_destroy(g_simple_dev->class, g_simple_dev->dev_no);
    class_destroy(g_simple_dev->class);
    cdev_del(&g_simple_dev->cdev);
    unregister_chrdev_region(g_simple_dev->dev_no, 1);
    kfree(g_simple_dev);
}

MODULE_LICENSE("GPL");
module_init(simple_cdev_init);
module_exit(simple_cdev_exit);
目录
相关文章
|
11天前
|
存储 算法 C语言
数据结构基础详解(C语言): 二叉树的遍历_线索二叉树_树的存储结构_树与森林详解
本文从二叉树遍历入手,详细介绍了先序、中序和后序遍历方法,并探讨了如何构建二叉树及线索二叉树的概念。接着,文章讲解了树和森林的存储结构,特别是如何将树与森林转换为二叉树形式,以便利用二叉树的遍历方法。最后,讨论了树和森林的遍历算法,包括先根、后根和层次遍历。通过这些内容,读者可以全面了解二叉树及其相关概念。
|
11天前
|
存储 机器学习/深度学习 C语言
数据结构基础详解(C语言): 树与二叉树的基本类型与存储结构详解
本文介绍了树和二叉树的基本概念及性质。树是由节点组成的层次结构,其中节点的度为其分支数量,树的度为树中最大节点度数。二叉树是一种特殊的树,其节点最多有两个子节点,具有多种性质,如叶子节点数与度为2的节点数之间的关系。此外,还介绍了二叉树的不同形态,包括满二叉树、完全二叉树、二叉排序树和平衡二叉树,并探讨了二叉树的顺序存储和链式存储结构。
|
25天前
|
Java Linux API
Linux设备驱动开发详解2
Linux设备驱动开发详解
23 6
|
25天前
|
消息中间件 算法 Unix
Linux设备驱动开发详解1
Linux设备驱动开发详解
23 5
|
23天前
|
NoSQL Linux Android开发
内核实验(三):编写简单Linux内核模块,使用Qemu加载ko做测试
本文介绍了如何在QEMU中挂载虚拟分区、创建和编译简单的Linux内核模块,并在QEMU虚拟机中加载和测试这些内核模块,包括创建虚拟分区、编写内核模块代码、编译、部署以及在QEMU中的加载和测试过程。
82 0
内核实验(三):编写简单Linux内核模块,使用Qemu加载ko做测试
|
25天前
|
Ubuntu NoSQL Linux
Linux内核和驱动
Linux内核和驱动
16 2
|
29天前
|
负载均衡 应用服务中间件 Linux
在Linux中,常用的 Nginx 模块有哪些,常来做什么?
在Linux中,常用的 Nginx 模块有哪些,常来做什么?
|
18天前
|
Linux API
Linux里的高精度时间计时器(HPET)驱动 【ChatGPT】
Linux里的高精度时间计时器(HPET)驱动 【ChatGPT】
|
27天前
|
关系型数据库 Linux PostgreSQL
【Azure 应用服务】Azure Function App Linux环境下的Python Function,安装 psycopg2 模块错误
【Azure 应用服务】Azure Function App Linux环境下的Python Function,安装 psycopg2 模块错误
|
1月前
|
Linux
【linux】【驱动】<specifier>-map-pass-thru讲解
【linux】【驱动】<specifier>-map-pass-thru讲解
12 0