linux创建create函数,linux class_create()函数应用

从linux内核2.6的某个版本之后,devfs不复存在,udev成为devfs的替代。相比devfs,udev有很多优势,在此就不罗嗦了,提醒一点,udev是应用层的东东,不要试图在内核的配置选项里找到它;加入对udev的支持很简单,以作者所写的一个字符设备驱动为例,在驱动初始化的代码里调用class_create为该设备创建一个class,再为每个设备调用 class_device_create创建对应的设备。大致用法如下:

struct class *myclass = class_create(THIS_MODULE, “my_device_driver”);

class_device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “my_device”);

这样的module被加载时,udev daemon就会自动在/dev下创建my_device设备文件。

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_create(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

`class_create_file` 函数Linux 内核中的一个函数,用于在 sysfs 文件系统中创建一个与设备驱动程序相关联的文件。该函数是在 `struct class` 结构体中定义的,函数原型如下: ```c int class_create_file(struct class *class, const struct class_attribute *attr); ``` 其中,`class` 是一个指向 `struct class` 结构体的指针,表示要创建 sysfs 文件系统节点的设备驱动程序所属的设备类;`attr` 是一个指向 `struct class_attribute` 结构体的指针,表示要创建的 sysfs 文件系统节点的属性信息。 `struct class_attribute` 结构体定义如下: ```c struct class_attribute { struct attribute attr; ssize_t (*show)(struct class *class, struct class_attribute *attr, char *buf); ssize_t (*store)(struct class *class, struct class_attribute *attr, const char *buf, size_t count); }; ``` 其中,`attr` 是一个 `struct attribute` 结构体,表示要创建的 sysfs 文件系统节点的属性名和权限等信息;`show` 和 `store` 分别是读取和写入 sysfs 文件系统节点的回调函数创建 sysfs 文件系统节点的流程如下: 1. 创建 `struct class_attribute` 结构体对象,设置其属性值。 2. 调用 `class_create_file` 函数,传入 `class` 和 `attr` 参数,创建 sysfs 文件系统节点。 3. 当用户空间程序读取或写入该节点时,内核会调用 `show` 或 `store` 回调函数,执行相应的操作。 总之,`class_create_file` 函数是设备驱动程序与用户空间程序交互的重要接口之一,可以实现设备驱动程序与用户空间程序之间的数据传输和控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值