設備驅動程序模型建立在幾個基本數據結構上,這些結構描述了總線、設備、設備驅動、屬性以及他們之間的關系。我們首先認識一下這些數據結構。
一、數據結構
設備表述符
[cpp]
- struct device {
- struct device *parent;/*指向父設備的指針*/
- /*該字段用於管理device和其他device結構,一起device
- 與其他結構之間的關系*/
- struct device_private *p;
-
- struct kobject kobj;/*內嵌的kobject結構*/
- const char *init_name; /* initial name of the device */
- struct device_type *type;
-
- struct semaphore sem; /* semaphore to synchronize calls to
- * its driver.
- */
-
- struct bus_type *bus;/*指向所連接總線的指針*/ /* type of bus device is on */
- struct device_driver *driver;/*指向控制設備驅動程序的指針*//* which driver has allocated this
- device */
- void *platform_data;/*指向遺留設備驅動程序的私有數據的指針*//* Platform specific data, device
- core doesn't touch it */
- struct dev_pm_info power;
-
- #ifdef CONFIG_NUMA
- int numa_node; /* NUMA node this device is close to */
- #endif
- u64 *dma_mask; /* dma mask (if dma'able device) */
- u64 coherent_dma_mask;/* Like dma_mask, but for
- alloc_coherent mappings as
- not all hardware supports
- 64 bit addresses for consistent
- allocations such descriptors. */
-
- struct device_dma_parameters *dma_parms;
-
- struct list_head dma_pools;/*www.linuxidc.com聚集的DMA緩沖池鏈表的首部*//* dma pools (if dma'ble) */
-
- struct dma_coherent_mem *dma_mem;/*指向設備所使用的一致性DMA存儲器描述符的指針*//* internal for coherent mem
- override */
- /* arch specific additions */
- struct dev_archdata archdata;
-
- dev_t devt; /* dev_t, creates the sysfs "dev" */
-
- spinlock_t devres_lock;
- struct list_head devres_head;
-
- struct klist_node knode_class;
- struct class *class;
- const struct attribute_group **groups; /* optional groups */
-
- void (*release)(struct device *dev);/*釋放設備描述符的回調函數*/
- };
Bus描述符
[cpp]
- struct bus_type {
- const char *name;/*總線類型名稱*/
- /*指向對象的指針,該對象包含總線屬性和用於導出此屬性到sysfs文件系統的方法*/
- struct bus_attribute *bus_attrs;
- /*指向對象的指針,該對象包含設備屬性和用於導出此屬性sysfs文件系統的方法*/
- struct device_attribute *dev_attrs;
- /*指向對象的指針,該對象包含設備驅動程序屬性和用於導出此屬性到sysfs文件
- 的方法*/
- struct driver_attribute *drv_attrs;
- /*檢驗給定的設備驅動程序是否支持特定設備的方法www.linuxidc.com*/
- int (*match)(struct device *dev, struct device_driver *drv);
- /**/
- int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
- int (*probe)(struct device *dev);
- int (*remove)(struct device *dev);
- void (*shutdown)(struct device *dev);
-
- int (*suspend)(struct device *dev, pm_message_t state);
- int (*resume)(struct device *dev);
-
- const struct dev_pm_ops *pm;
-
- struct bus_type_private *p;
- };
設備驅動
[cpp]
- /*設備驅動程序模型中的每個驅動程序*/
- struct device_driver {
- const char *name;/*設備驅動程序的名稱*/
- struct bus_type *bus;/*指向總線描述符的指針,總線連接所支持的設備*/
-
- struct module *owner;/*標識實現設備程序的模塊,如果有的話*/
- const char *mod_name; /* used for built-in modules */
-
- bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
-
- int (*probe) (struct device *dev);/*探測設備的方法(檢驗設備驅動程序是否可以控制該設備)*/
- int (*remove) (struct device *dev);/*移走設備時所調用的方法*/
- void (*shutdown) (struct device *dev);/*設備斷電時所調用的方法*/
- int (*suspend) (struct device *dev, pm_message_t state);/*設備置於低功率狀態時所調用的方法*/
- int (*resume) (struct device *dev);/*設備恢復正常狀態時所調用的方法*/
- const struct attribute_group **groups;
-
- const struct dev_pm_ops *pm;
-
- struct driver_private *p;
- };
類描述符
[cpp]
- /*
- * device classes
- */
- struct class {
- const char *name;
- struct module *owner;
-
- struct class_attribute *class_attrs;
- struct device_attribute *dev_attrs;
- struct kobject *dev_kobj;
-
- int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
- char *(*devnode)(struct device *dev, mode_t *mode);
-
- void (*class_release)(struct class *class);
- void (*dev_release)(struct device *dev);
-
- int (*suspend)(struct device *dev, pm_message_t state);
- int (*resume)(struct device *dev);
-
- const struct dev_pm_ops *pm;
-
- struct class_private *p;
- };