Heng30的博客
搜索 分类 关于 订阅

如何给Linux字符设备驱动创建多个从设备文件?

2025-02-28

一个驱动可以管理多个设备文件,通过不同的从设备号来区分它们。下面就提供了实例代码实现如何创建多个从设备文件和区分不同的从设备文件的访问请求。

实例代码

// simple_cdev.c

#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/device/class.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>

#define BUFFER_SIZE 1024
#define DEV_MINOR_COUNTS 3
#define DEV_NAME "simple_cdev"

typedef struct {
    struct cdev *cdev;        // 字符设备
    struct class *class;      // 设备类
    struct device *dev;       // /dev目录下的设备
    char buffer[BUFFER_SIZE]; // 缓冲区
} simple_cdev_t;

static simple_cdev_t my_cdev = {0};

static int _open(struct inode *inode, struct file *file) {
    pr_info("simple_cdev_open\n");
    pr_info("major = %d, minor = %d\n", imajor(inode), iminor(inode));

    return 0;
}

static ssize_t _read(struct file *file, char __user *userbuf, size_t size,
                     loff_t *offset) {
    pr_info("simple_cdev_read\n");

    size = min(size, BUFFER_SIZE);

    unsigned long remain_len = copy_to_user(userbuf, my_cdev.buffer, size);

    if (remain_len > 0) {
        pr_err("copy_to_user failed\n");
        return -EIO;
    }

    return size;
}

static ssize_t _write(struct file *file, const char __user *userbuf,
                      size_t size, loff_t *offset) {
    pr_info("simple_cdev_write\n");

    size = min(size, BUFFER_SIZE);
    unsigned long remain_len = copy_from_user(my_cdev.buffer, userbuf, size);

    if (remain_len > 0) {
        pr_err("copy_from_user failed\n");
        return -EIO;
    }

    return size;
}

static int _release(struct inode *inode, struct file *file) {
    pr_info("simple_cdev_release\n");
    return 0;
}

static struct file_operations fops = {
    .open = _open,
    .read = _read,
    .write = _write,
    .release = _release,
};

static int __init simple_cdev_init(void) {
    pr_info("simple_cdev_init\n");

    my_cdev.cdev = cdev_alloc();
    if (!my_cdev.cdev) {
        pr_err("simple_cdev cdev_alloc failed!\n");
        return -ENOMEM;
    }

    // 初始化字符设备
    cdev_init(my_cdev.cdev, &fops);

    // 申请设备号, 由系统分配主设备号和3个从设备号
    int ret =
        alloc_chrdev_region(&my_cdev.cdev->dev, 0, DEV_MINOR_COUNTS, DEV_NAME);

    if (ret) {
        pr_info("simple_cdev alloc_chrdev_region failed!\n");
        return ret;
    }

    pr_info("simple_cdev major = %d\n", MAJOR(my_cdev.cdev->dev));

    // 添加到内核
    ret = cdev_add(my_cdev.cdev, my_cdev.cdev->dev, DEV_MINOR_COUNTS);
    if (ret) {
        pr_info("simple_cdev cdev_add failed!\n");
        return ret;
    }

    // 申请设备类, 会在/sys/class目录下常见一个simple_cdev的目录
    my_cdev.class = class_create("simple_cdev");
    if (IS_ERR(my_cdev.class)) {
        pr_err("simple_cdev class_create failed\n");
        return PTR_ERR(my_cdev.class);
    }

    // 申请设备对象,会在/dev目录下创建设备文件simple_cdev,
    // 并且在/sys/class/simple_cdev目录下常见一个simple_cdev的设备节点
    for (int i = 0; i < DEV_MINOR_COUNTS; i++) {
        char dev_name[64];
        snprintf(dev_name, sizeof(dev_name), "%s%d", DEV_NAME, i);

        my_cdev.dev = device_create(
            my_cdev.class, NULL,
            MKDEV(MAJOR(my_cdev.cdev->dev), MINOR(my_cdev.cdev->dev) + i), NULL,
            dev_name);

        if (IS_ERR(my_cdev.dev)) {
            pr_err("simple_cdev device_create failed\n");
            return PTR_ERR(my_cdev.dev);
        }
    }

    return 0;
}

static void __exit simple_cdev_exit(void) {
    pr_info("simple_cdev_exit\n");

    for (int i = 0; i < DEV_MINOR_COUNTS; i++) {
        device_destroy(my_cdev.class, MKDEV(MAJOR(my_cdev.cdev->dev),
                                            MINOR(my_cdev.cdev->dev) + i));
    }

    class_destroy(my_cdev.class);
    cdev_del(my_cdev.cdev);
    unregister_chrdev_region(my_cdev.cdev->dev, DEV_MINOR_COUNTS);
    kfree(my_cdev.cdev);
}

module_init(simple_cdev_init);
module_exit(simple_cdev_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("heng30");
MODULE_VERSION("v0.0.1");
MODULE_DESCRIPTION("A simple_cdev kernel module");

Makefile编译脚本

#!/bin/sh

top-dir = $(shell pwd)
kernel-version = $(shell uname -r)
kernel-dir ?= /lib/modules/$(kernel-version)/build

obj-m += simple_cdev.o

all:
        make -C $(kernel-dir) modules M=$(top-dir)

clean:
        rm -f *.o *.ko *.mod *.mod.c *.order *.symvers
        make -C $(kernel-dir) clean m=$(top-dir)

测试

  • 安装驱动:insmod simple_cdev.ko

  • 查看simple_cdev设备文件:ls -al /dev/simple_cdev*

crw------- 1 root root 236, 0 Feb 28 11:46 /dev/simple_cdev0
crw------- 1 root root 236, 1 Feb 28 11:46 /dev/simple_cdev1
crw------- 1 root root 236, 2 Feb 28 11:46 /dev/simple_cdev2
  • 移除驱动:rmmod simple_cdev.ko