【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);
目录
相关文章
|
16天前
|
Java Linux API
Linux设备驱动开发详解2
Linux设备驱动开发详解
22 6
|
16天前
|
存储 缓存 Unix
Linux 设备驱动程序(三)(上)
Linux 设备驱动程序(三)
15 3
|
16天前
|
Linux
Linux 设备驱动程序(四)
Linux 设备驱动程序(四)
9 1
|
16天前
|
存储 数据采集 缓存
Linux 设备驱动程序(三)(中)
Linux 设备驱动程序(三)
13 1
|
16天前
|
存储 前端开发 大数据
Linux 设备驱动程序(二)(中)
Linux 设备驱动程序(二)
15 1
|
16天前
|
缓存 安全 Linux
Linux 设备驱动程序(二)(上)
Linux 设备驱动程序(二)
16 1
|
9天前
|
Linux API
Linux里的高精度时间计时器(HPET)驱动 【ChatGPT】
Linux里的高精度时间计时器(HPET)驱动 【ChatGPT】
|
16天前
|
存储 缓存 安全
Linux 设备驱动程序(三)(下)
Linux 设备驱动程序(三)
11 0
|
16天前
|
安全 Linux 程序员
Linux 设备驱动程序(二)(下)
Linux 设备驱动程序(二)
10 0
|
2天前
|
存储 C语言
数据结构基础详解(C语言): 栈与队列的详解附完整代码
栈是一种仅允许在一端进行插入和删除操作的线性表,常用于解决括号匹配、函数调用等问题。栈分为顺序栈和链栈,顺序栈使用数组存储,链栈基于单链表实现。栈的主要操作包括初始化、销毁、入栈、出栈等。栈的应用广泛,如表达式求值、递归等场景。栈的顺序存储结构由数组和栈顶指针构成,链栈则基于单链表的头插法实现。