这篇是我学习时遇见的struct device内容的集合,记录着所学时对此结构体的理解,内容不完善,会不断更新
在学习Linux设备驱动时,经常遇见的是就是struct device结构体,他是保存设备基本信息的结构体。几乎在所有的驱动中都会遇见,意思就是他是驱动的设备结构体,所有的各种类型的结构体都继承他。在include/linux/device.h的723行有定义,其内容如下:
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 this
device */
void *platform_data; /* Platform specific data, device
core doesn't touch it */
void *driver_data; /* Driver data, set and get with
dev_set/get_drvdata */
struct dev_pm_info power;
struct dev_pm_domain *pm_domain;
#ifdef CONFIG_PINCTRL
struct dev_pin_info *pins;
#endif
#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. */
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 mem
override */
#ifdef CONFIG_DMA_CMA
struct cma *cma_area; /* contiguous memory area for dma
allocations */
#endif
/* arch specific additions */
struct dev_archdata archdata;
struct device_node *of_node; /* associated device tree node */
struct fwnode_handle *fwnode; /* firmware 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;
};
2020/3/27更新 :struct device内容有这个:
* @of_node: Associated device tree node.
struct device_node *of_node;
这是struct device_node类型,保存着当前设备结点信息。上面是此变量的注释,大概意思就是 关联设备树节点。比如,当获得i2c设备信息时(i2c设备信息结构体struct i2c_client),i2c_client结构体里面有struct device device变量,此device的of_node变量就是当前在设备树中此i2c设备结点,就可以通过此节点获取设备树中此节点的任何信息。
2020/3/28更新:
. @parent: The device's "parent" device, the device to which it is attached.
* In most cases, a parent device is some sort of bus or host
* controller. If parent is NULL, the device, is a top-level device,
* which is not usually what you want.
struct device *parent;
这也是struct device类型,表示当前设备的父设备。上面是此变量的注释,大概意思就是当前设备所连接的上层设备叫父设备,大多数情况下,父设备是总线或者主机控制器,如果父设备是空的,表示这是最顶层的设备。比如:我现在input子系统驱动屏幕触控IC,触控IC所使用的总线是I2C,所示可表示:input输入子系统设备的父设备是屏幕触控IC,而触控IC的父设备是I2C总线控制器。