Linux裝置模型組件—類裝置

來源:互聯網
上載者:User
 
一、定義:一個裝置類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檔案中;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.