您的位置:首页 > 移动开发 > Objective-C

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

2013-11-24 14:32 295 查看
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 *);

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: