class_device_create class_device_destroy

自学驱动以来,一直都是在加载模块后采用手动创建节点,虽然这个过程比较简单,毕竟还是有点麻烦,尤其是在调试模块的时候。 #insmod module_name.ko #mknod /dev/module_name c MAJOR MINOR # 在2.4里设备文件采用的是devfs,在2.6里已经用udev取代devfs,为解决上面那样手动创建节点的麻烦,我们可以在程序里加上创建节点这项,如下: 以字符设备char_dev为例,在驱动初始化的代码里调用class_create为该设备创建一个class,再为每个设备调用 class_device_create创建对应的设备,这样的module被加载时,undev daemon就会自动在/dev下创建char_dev设备文件。大概方法如下: struct class *myclass = class_create(THIS_MODULE, “char_dev”);
class_device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “char_dev”);

当然,在exit函数中要把创建的class移除:
class_destory(&xxx_dev->cdev);
class_device_desotry(my_class,MKDEV(major_num,0));

下面介绍下函数class_creat和class_device_creat的原型:  class_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class *class_create(struct module *owner, const char *name)
    class_create - create a struct class structure
    @owner: pointer to the module that is to "own" this struct class
    @name: pointer to a string for the name of this class.
在/sys/class/下创建类目录


class_device_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class_device *class_device_creat(struct class        *cls,
                                         struct class_device *parent,
                                         dev_t               devt,
                                         struct device       *device,
                                         const char          *fmt, ...)

    class_device_create - creates a class device and registers it with sysfs
    @cls: pointer to the struct class that this device should be registered to.
    @parent: pointer to the parent struct class_device of this new device, if any.
    @devt: the dev_t for the char device to be added.
    @device: a pointer to a struct device that is assiociated with this class device.
    @fmt: string for the class device's name
void class_destroy(struct class *cls);/*销毁/sys/class下的类   */ void class_device_destroy(struct class *cls, dev_t devt);   /*销毁一个类设备*/ 参数含义同上 补充: 在Linux2.6中,针对上面的这个问题不同的版本有些修改,使用前要先查看下/.../include/linux/device.h里的函数声明,如我用的是Linux2.6.29,里面就没有class_device_create函数,而直接使用device_create就可以了,而在之前的版本如Linux2.6.15,里面就要用class_device_create函数 。
### 解决 `class_create` 和 `THIS_MODULE` 报错的方法 当在较新的Linux内核版本中使用 `class_create()` 函数并传入 `THIS_MODULE` 参数时遇到编译错误,这通常是因为内核API的变化所致。自2.6.29版起,内核更新了设备模型接口,引入了新函数来替代旧有的实现方式。 对于现代内核版本,在创建类对象时应采用如下做法: - 使用 `class_create()` 而不是过时的 `class_device_create()` - 确认已包含必要的头文件 `<linux/device.h>` 以便访问最新的定义和声明[^1] 具体来说,如果尝试传递宏 `THIS_MODULE` 给 `class_create()` 并遭遇失败,则可能是由于参数列表不匹配引起的。此时应当检查所使用的内核文档以及源码中的实际签名,以确保调用形式正确无误。根据 `/include/linux/device.h` 中的内容显示,`class_create()` 的原型确实接受一个指向模块结构体指针作为其第一个参数,因此直接提供 `THIS_MODULE` 是合理的操作[^2]。 然而,考虑到不同发行版可能存在的差异性,建议采取以下措施进一步排查问题根源: - 核实当前开发环境下的确切内核版本号 - 查阅对应版本的具体手册页或官方指南获取最权威的信息指导 - 尝试简化测试案例重现该现象,并通过调试工具定位具体的语法或语义层面的问题所在 下面给出一段示范性的代码片段用于展示正确的 `class_create` 应用场景: ```c #include <linux/module.h> #include <linux/init.h> #include <linux/device.h> static struct class *my_class; static int __init my_module_init(void){ printk(KERN_INFO "Initializing module\n"); // 正确的方式:指定所有者为 THIS_MODULE my_class = class_create(THIS_MODULE, "example"); if (IS_ERR(my_class)) { pr_err("Failed to create class\n"); return PTR_ERR(my_class); } return 0; } static void __exit my_module_exit(void){ printk(KERN_INFO "Cleaning up module\n"); if (!IS_ERR_OR_NULL(my_class)) class_destroy(my_class); } module_init(my_module_init); module_exit(my_module_exit); MODULE_LICENSE("GPL"); ``` 上述实例展示了如何安全地初始化一个新的类别,并且包含了清理逻辑以防资源泄露。值得注意的是,这里假设读者已经熟悉基本的Makefile配置和其他辅助设置过程[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值