Linux那些事儿之我是Sysfs(3)设备模型上层容器
§1 bus
系統(tǒng)中總線由struct bus_type描述,定義為:
include/linux/device.h
struct bus_type {const char *name;總線類型的名稱const char *dev_name;struct device *dev_root;struct device_attribute *dev_attrs; /* use dev_groups instead */const struct attribute_group **bus_groups;const struct attribute_group **dev_groups;const struct attribute_group **drv_groups;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 (*online)(struct device *dev);int (*offline)(struct device *dev);int (*suspend)(struct device *dev, pm_message_t state);int (*resume)(struct device *dev);const struct dev_pm_ops *pm;const struct iommu_ops *iommu_ops;struct subsys_private *p;struct lock_class_key lock_key;
};
每個bus_type對象都對應/sys/bus目錄下的一個子目錄,如PCI總線類型對應于/sys/bus/pci。在每個這樣的目錄下都存在兩個子目錄:devices和drivers(dev_groups和drv_groups)。其中devices子目錄描述連接在該總線上的所有設備,而drivers目錄則描述與該總線關聯(lián)的所有驅(qū)動程序。與device_driver對象類似,bus_type結(jié)構(gòu)還包含幾個函數(shù)(match()、hotplug()等)處理相應的熱插拔、即插即拔和電源管理事件。
§2 device
系統(tǒng)中的任一設備在設備模型中都由一個device對象描述,其對應的數(shù)據(jù)結(jié)構(gòu)struct device
定義為:
include/linux/device.h
struct device {struct device *parent;struct device_private *p;struct kobject kobj;const char *init_name; /* initial name of the device */const struct device_type *type;struct mutex mutex; /* mutex to synchronize calls to* its driver.*/struct bus_type *bus; /* type of bus device is on */struct device_driver *driver; /* which driver has allocated thisdevice */void *platform_data; /* Platform specific data, devicecore doesn't touch it */void *driver_data; /* Driver data, set and get withdev_set/get_drvdata */struct dev_pm_info power;struct dev_pm_domain *pm_domain;#ifdef CONFIG_PINCTRLstruct dev_pin_info *pins;
#endif#ifdef CONFIG_NUMAint numa_node; /* NUMA node this device is close to */
#endifu64 *dma_mask; /* dma mask (if dma'able device) */u64 coherent_dma_mask;/* Like dma_mask, but foralloc_coherent mappings asnot all hardware supports64 bit addresses for consistentallocations such descriptors. */unsigned long dma_pfn_offset;struct device_dma_parameters *dma_parms;struct list_head dma_pools; /* dma pools (if dma'ble) */struct dma_coherent_mem *dma_mem; /* internal for coherent memoverride */
#ifdef CONFIG_DMA_CMAstruct cma *cma_area; /* contiguous memory area for dmaallocations */
#endif/* arch specific additions */struct dev_archdata archdata;struct device_node *of_node; /* associated device tree node */struct acpi_dev_node acpi_node; /* associated ACPI device node */dev_t devt; /* dev_t, creates the sysfs "dev" */u32 id; /* device instance */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);struct iommu_group *iommu_group;bool offline_disabled:1;bool offline:1;
};
parent域指向父對象。Device對象還內(nèi)嵌一個kobject對象,用于引用計數(shù)管理并通過它實現(xiàn)設備層次結(jié)構(gòu)。Driver域指向管理該設備的驅(qū)動程序?qū)ο?#xff0c;而driver data則是提供給驅(qū)動程序的數(shù)據(jù)。Bus域描述設備所連接的總線類型。
內(nèi)核提供了相應的函數(shù)用于操作device對象。其中device_register()函數(shù)將一個新的device對象插入設備模型,并自動在/sys/devices下創(chuàng)建一個對應的目錄。device_unregister()完成相反的操作,注銷設備對象。get_device()和put_device()分別增加與減少設備對象的引用計數(shù)。通常device結(jié)構(gòu)不單獨使用,而是包含在更大的結(jié)構(gòu)中作為一個子結(jié)構(gòu)使用,比如描述PCI設備的struct pci_dev,還有我們ldd_dev,其中的dev域就是一個device對象。
§3. driver
系統(tǒng)中的每個驅(qū)動程序由一個device_driver對象描述,對應的數(shù)據(jù)結(jié)構(gòu)定義為:
include/linux/device.h
struct device_driver {const char *name;設備驅(qū)動程序的名稱struct bus_type *bus;該驅(qū)動所管理的設備掛接的總線類型struct module *owner;const char *mod_name; /* used for built-in modules */bool suppress_bind_attrs; /* disables bind/unbind via sysfs */const struct of_device_id *of_match_table;const struct acpi_device_id *acpi_match_table;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;
};
內(nèi)核提供類似的函數(shù)用于操作device_driver對象,如get_driver()增加引用計數(shù),driver_register()用于向設備模型插入新的driver對象,同時在sysfs文件系統(tǒng)中創(chuàng)建對應的目錄。device_driver()結(jié)構(gòu)還包括幾個函數(shù),用于處理熱拔插、即插即用和電源管理事件。
可能你面對剛剛列舉出來的一些列數(shù)據(jù)結(jié)構(gòu),感到很苦惱,很莫名其妙。沒關系,我接下來講個例子您就明白了。
總結(jié)
以上是生活随笔為你收集整理的Linux那些事儿之我是Sysfs(3)设备模型上层容器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2024年春假方案骂声一片,放不起别放,
- 下一篇: Linux那些事儿之我是Sysfs(7)