免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8115 | 回复: 3
打印 上一主题 下一主题

最近看Linux device model的笔记 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-27 14:44 |只看该作者 |倒序浏览
仍然是以图表为主。

大家多提意见啊。


device model

device model主要的数据结构有三个: kobject, kset 和 subsystem.



相关数据结构:

struct kobject {
        char                        * k_name;
        char                        name[KOBJ_NAME_LEN];
        struct kref                kref;
        struct list_head        entry;
        struct kobject                * parent;
        struct kset                * kset;
        struct kobj_type        * ktype;
        struct dentry                * dentry;
};


struct kset {
        struct subsystem        * subsys;
        struct kobj_type        * ktype;
        struct list_head        list;
        spinlock_t                list_lock;
        struct kobject                kobj;
        struct kset_hotplug_ops        * hotplug_ops;
};
  
struct subsystem {
        struct kset                kset;
        struct rw_semaphore        rwsem;
};


他们是整个device model的骨架(用面向对象的口气说:这叫基类(不是鸡肋^_`))。

其他所有的结构,比如cdev,bus等等,都是在这些骨架上搭建起来的。

kobject是最最基础的。很多结构都会有一个内嵌的kobject.
kobject的引用计数功能是最重要的。


kset可以看作是一类kojbect的集合,而subsystem可以看作是若干kset的集合。



多个kset可以通过subsys指针指向一个subsystem。但subsystem仅包含一个内嵌的kset。
这种单向关系意味着不可能仅仅通过一个subsystem结构体就找到该subsystem所有的ksets。

他们的联系示意图如下:
                                                         
            


另外需要说明的是,kset中的list是贯穿所有kobject的双向链表,但是并不一定是通过kobject的entry字段穿在一起的。
所有的kobject并不简单的是一个独立的kobject,而可能是内嵌在其他数据结构中的kobject(例如kset,subsystem 等等)

            
               
                 
所有的kset一定属于一个subsystem中(kset的subsys指向某一个subsystem)
对于subsystem内嵌的kset,如果没有设定它的subsys, 则是指向自身的subsystem。(见subsystem_register()函数)

论坛徽章:
0
2 [报告]
发表于 2008-05-27 14:45 |只看该作者
device

相关数据结构:

struct device {
        int                 dev_type;            // PCICA, PCICC, PCIXCC_MCL2,
                                            // PCIXCC_MCL3, CEX2C
        enum devstat         dev_stat;            // current device status
        int                 dev_self_x;            // Index in array
        int                 disabled;            // Set when device is in error
        int                 user_disabled;            // Set when device is disabled by user
        int                 dev_q_depth;            // q depth
        unsigned char *         dev_resp_p;            // Response buffer address
        int                 dev_resp_l;            // Response Buffer length
        int                 dev_caller_count;  // Number of callers
        int                 dev_total_req_cnt; // # requests for device since load
        struct list_head dev_caller_list;   // List of callers
};



device model中的设备用device对象表示。
device对象全都在devices_subsys子系统中。对应sysfs中的/sys/devices目录

设备是按照层次关系组织的。
                                                
                                                

                                                               
device中内嵌的kobject - kobj 也能够反应出层次关系。

论坛徽章:
0
3 [报告]
发表于 2008-05-27 14:47 |只看该作者
device_driver

相关数据结构为:

struct device_driver {
        char                        * name;
        struct bus_type                * bus;

        struct completion        unloaded;
        struct kobject                kobj;
        struct list_head        devices;

        struct module                 * owner;

        int        (*probe)        (struct device * dev);
        int         (*remove)        (struct device * dev);
        void        (*shutdown)        (struct device * dev);
        int        (*suspend)        (struct device * dev, pm_message_t state, u32 level);
        int        (*resume)        (struct device * dev, u32 level);
};



device model中的驱动程序由device_driver对象表示。
若干个同类的设备可以共用同一个驱动程序。

论坛徽章:
0
4 [报告]
发表于 2008-05-27 14:48 |只看该作者
bus_type

相关数据结构:

struct bus_type {
        char                        * name;

        struct subsystem        subsys;
        struct kset                drivers;
        struct kset                devices;

        struct bus_attribute        * bus_attrs;
        struct device_attribute        * dev_attrs;
        struct driver_attribute        * drv_attrs;

        int                (*match)(struct device * dev, struct device_driver * drv);
        int                (*hotplug) (struct device *dev, char **envp,
                                    int num_envp, char *buffer, int buffer_size);
        int                (*suspend)(struct device * dev, pm_message_t state);
        int                (*resume)(struct device * dev);
};


内核所支持的每一种总线类型都由一个bus_type对象表示。
bus_type中内嵌了一个subsystem - subsys。
系统中的bus_subsys子系统将所有的bus_type中的subsys集合在一起。
bus_subsys对应sysfs中的/sys/bus目录.

另外,bus_type中有两个内嵌的kset对象:devices 和 drivers。分别表示该bus上的设备和驱动。





函数bus_for_each_dev() 和 bus_for_each_drv()分别用于遍历bus上devices和drivers链表中的所有元素。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP