国嵌——内核驱动模型——第二天kobject

来源:互联网 发布:淘宝买百度云 编辑:程序博客网 时间:2024/05/29 10:44
sysfs文件系统:
sysfs被挂载在/sys/目录

block目录:
块设备,里面有属性文件,描述块设备各方面的属性
(loop块设备是使用文件来模拟的)

bus目录:内核中注册的每条总线,如:
 ide pci scsi usb pcmcia
每个总线目录内又包含两个子目录:
devices和drivers

class目录:将设备按照功能进行分类,如:/sys/class/net网络

devices目录:包含系统所有的设备

kernel目录:内核中的配置参数

module目录:系统所有模块的信息

firmware目录:系统中的固件

fs目录:描述系统中的文件系统

power目录:系统中电源选项

kobject实现了基本的面向对象管理机制,是构成Linux2.6设备模型的
核心结构。它与sysfs文件系统紧密相连,在内核中注册的每个kobject
对象对应sysfs文件系统中的一个目录。
#include <linux/kobject.h>

struct kobject {
    const char        *name;//在/sys/目录下生成的文件名字
    struct list_head    entry;
    struct kobject        *parent;//指向父对象
    struct kset        *kset;
    struct kobj_type    *ktype;
    struct sysfs_dirent    *sd;
    struct kref        kref;//对象引用计数
    unsigned int state_initialized:1;
    unsigned int state_in_sysfs:1;
    unsigned int state_add_uevent_sent:1;
    unsigned int state_remove_uevent_sent:1;
    unsigned int uevent_suppress:1;
};

kobject操作:
void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
int kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...);
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, struct kobject *parent, const char *fmt, ...);
void kobject_del(struct kobject *kobj);
void kobject_put(struct kobject *kobj);//引用计数-1,为0时调用release释放对象
struct kobject *kobject_get(struct kobject *kobj);//引用计数+1

struct kobj_type {
    void (*release)(struct kobject *kobj);//用于释放kobject占用的资源,当kobject引用计数为0时被调用
    const struct sysfs_ops *sysfs_ops;
    struct attribute **default_attrs;
    const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
    const void *(*namespace)(struct kobject *kobj);
};

struct attribute {
    const char        *name;//属性文件名
    mode_t            mode;   //属性的保护位,见<linux/stat.h>
#ifdef CONFIG_DEBUG_LOCK_ALLOC
    struct lock_class_key    *key;
    struct lock_class_key    skey;
#endif
};

struct sysfs_ops {
    ssize_t    (*show)(struct kobject *kobj, struct attribute *attr,char *buffer);
//当用户读属性文件时,该函数被调用,该函数将属性值存入buffer中返回给用户
    ssize_t    (*store)(struct kobject *kobj,struct attribute *attr,const char *buffer, size_t count);
//当用户写属性文件时,该函数被调用,用于存储用户传入的属性值
    const void *(*namespace)(struct kobject *, const struct attribute *);
};


原创粉丝点击