总线、设备、驱动、类

本文深入探讨了Linux内核中的总线、设备、驱动和类的概念及其相互关系。详细介绍了相关结构体,如总线类型、设备、驱动结构体等,并阐述了系统的初始化过程,包括driver_init、devices_init等步骤。同时,讲解了注册与注销流程,如设备的device_add、driver_probe_device等,以及类的注册与销毁。最后,通过platform总线的例子说明了如何搭建总线设备驱动类框架。
摘要由CSDN通过智能技术生成

一. 总线、设备、驱动、类各自的相关结构体

    1.总线

     1.1 总线类型结构体

struct bus_type {
	const char	*name;	//总线类型名
	struct bus_attribute	*bus_attrs;	//总线属性
	struct device_attribute	*dev_attrs;	//设备属性
	struct driver_attribute	*drv_attrs;	//驱动属性
	int (*match)(struct device *dev, struct device_driver *drv);	//match方法
	int (*uevent)(struct device *dev, struct kobj_uevent_env *env);	//uevent方法
	int (*probe)(struct device *dev);		//总线probe方法
	int (*remove)(struct device *dev);		//总线remove方法
	void (*shutdown)(struct device *dev);	//总线remove方法
	int (*suspend)(struct device *dev, pm_message_t state);	//总线shutdown方法
	int (*resume)(struct device *dev);		//总线resume方法
	const struct dev_pm_ops *pm;	//总线电源管理函数集
	struct bus_type_private *p;		//总线私有数据
};

    1.2 总线私有数据结构体

struct bus_type_private {
	struct kset subsys;
	struct kset *drivers_kset;
	struct kset *devices_kset;
	struct klist klist_devices;
	struct klist klist_drivers;
	struct blocking_notifier_head bus_notifier;
	unsigned int drivers_autoprobe:1;
	struct bus_type *bus;
};

     1.3 总线类型属性结构体

struct bus_attribute {
	struct attribute	attr;
	ssize_t (*show)(struct bus_type *bus, char *buf);
	ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
};

     1.4 设备属性结构体

struct device_attribute {
	struct attribute	attr;
	ssize_t (*show)(struct device *dev, struct device_attribute *attr,char *buf);
	ssize_t (*store)(struct device *dev, struct device_attribute *attr,const char *buf, size_t count);
};

     1.5 设备驱动属性结构体

struct driver_attribute {
	struct attribute attr;
	ssize_t (*show)(struct device_driver *driver, char *buf);
	ssize_t (*store)(struct device_driver *driver, const char *buf,size_t count);
};

 

     2.设备

     2.1 设备结构体

struct device {
	struct device	*parent;	//父设备指针
	struct device_private	*p;	//设备私有数据
	struct kobject kobj;
	const char	*init_name; /* initial name of the device */
	struct device_type	*type;
	struct mutex	mutex;
	struct bus_type	*bus;	/* type of bus device is on */
	struct device_driver *driver;	//匹配的设备驱动
	void	*platform_data;	//平台数据	
	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;
	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 */
	/* arch specific additions */
	struct dev_archdata	archdata;
#ifdef CONFIG_OF
	struct device_node	*of_node;
#endif
	dev_t	devt;	//设备号
	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);	//release方法
};

     2.2 设备私有数据结构体

struct device_private {
	struct klist klist_children;
	struct klist_node knode_parent;
	struct klist_node knode_driver;
	struct klist_node knode_bus;
	void *driver_data;
	struct device *device;
};

 

     3.驱动

     3.1 设备驱动结构体

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 */
#if defined(CONFIG_OF)
	const struct of_device_id	*of_match_table;
#endif
	int (*probe) (struct device *dev);	//probe方法
	int (*remove) (struct device *dev);	//remove方法
	void (*shutdown) (struct device *dev);	//shutdown方法
	int (*suspend) (struct device *dev, pm_message_t state);	//suspend方法
	int (*resume) (struct device *dev);	//resume方法
	const struct attribute_group **groups;
	const struct dev_pm_ops *pm;	//电源管理函数集
	struct driver_private *p;	//设备驱动私有数据
};

     3.2 设备驱动私有数据结构体

struct driver_private {
	struct kobject kobj;
	struct klist klist_devices;
	struct klist_node knode_bus;
	struct module_kobject *mkobj;
	struct device_driver *driver;
};

 

     4.

     4.1 设备类结构体

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 kobj_ns_type_operations *ns_type;
	const void *(*namespace)(struct device *dev);
	const struct dev_pm_ops *pm;	//电源管理函数集
	struct class_priva
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值