一、定义:
一个设备类struct class的真正目的是作为一个该类具体实例(设备)的容器使用;一个设备类的具体实例由struct class_device结构来描述;也可以这样理解:struct class类型相当于面向对象系统中的类的概念,而struct class_device类型相当于面向对象系统中的实例对象的概念;只有在应用具体实例对象的时候,它的类才有意义;类设备struct class_device结构定义如下:
struct class_device
{
struct list_head node;
struct kobject kobj; //内嵌的kobject对象
struct class* class; //所属的设备类class
dev_t devt; //设备编号
struct class_device_attribute* devt_attr; //类设备属性
struct class_device_attribute uevent_attr; //类设备事件属性
struct device* dev; //如果存在,则创建到/sys/devices目录下相应入口的符号链接
void* class_data; //类私有数据
struct class_device* parent; //父设备,即,当前设备所附着到的设备
struct attribute_group** groups; /* optional groups */
void (*release)(struct class_device* dev);
int (*uevent)(struct class_device* dev, char** envp, int num_envp, char* buffer, int buffer_size);
char class_id[BUS_ID_SIZE]; //类唯一标识
};
二、类设备相关函数
int class_device_register(struct class_device* cd):类设备注册;
void class_device_unregister(struct class_device* cd):类设备注销;
int class_device_rename(struct class_device* cd, char* new_name):类设备重命名;
void class_device_initialize(struct class_device* cd):类设备初始化;
int class_device_add(struct class_device* cd):把类设备对象加入到设备模型的层次结构中;
void class_device_del(struct class_device* cd):把类设备对象从设备模型的层次结构中删除;
struct class_device * class_device_get(struct class_device* cd):给类设备对象增加引用计数;
void class_device_put(struct class_device* cd):给类设备对象减少引用计数;
备注:每一个设备类class对象都包含一个类设备class_device对象的链表,而每一个类设备class_device对象又表示一个逻辑设备,并通过类设备struct class_device结构中的dev成员(一个指向struce device的指针)关联到一个物理设备上;这样,一个逻辑设备总是对应于一个物理设备,但是,一个物理设备却可能对应多个逻辑设备;
三、类设备属性:
struct class_device_attribute
{
struct attribute attr;
ssize_t (*show)(struct class_device* cls, char* buf);
ssize_t (*store)(struct class_device* cls, const char* buf, size_t count);
};
int class_device_create_file(struct class_device* cd, const struct class_device_attribute* attr):为指定类设备增加指定属性;
void class_device_remove_file(struct class_device* cd, const struct class_device_attribute* attr):删除指定类设备上的指定属性;
int class_device_create_bin_file(struct class_device* cd, struct bin_attribute* attr);
void class_device_remove_bin_file(struct class_device* cd, struct bin_attribute* attr);
四、类接口:
当设备加入或离开类时,将引发class_interface中的成员函数被调用;
struct class_interface
{
struct list_head node;
struct class* class; //对应的设备类class
int (*add)(struct class_device* cd, struct class_interface* ci); //设备加入时触发
void (*remove)(struct class_device* cd, struct class_interface* ci); //设备移除时触发
};
int class_interface_register(struct class_interface* ci):类接口注册;
void class_interface_unregister(struct class_interface* ci):类接口注销;
struct class* class_create(struct module* owner, char* name):创建类;
void class_destroy(struct class* cls):销毁类;
//创建类设备
struct class_device *class_device_create(struct class *cls, struct class_device *parent, dev_t devt, struct device *device, char *fmt, ...);
//销毁类设备
void class_device_destroy(struct class *cls, dev_t devt);
备注:struct class_device、struct class_device_attribute、struct class_interface结构均定义在/usr/src/kernels/2.6.18-8.el5-i686/include/linux/device.h文件中;
Linux设备模型组件---类设备
最新推荐文章于 2025-08-19 17:11:49 发布