linux提供了新的设备模型:总线(bus)、设备(device)、驱动(driver)。其中总线是处理器与设备之间通道,在设备模型中,所有的设备都通过总线相连;设备是对于一个设备的详细信息描述,驱动是设备的相关驱动。其基本关系如下:bus 相当于一个容器,是device 和device_driver 的管理机构,它包含了一个device 集合和一个driver 集合。其中,device集合包含了挂在该总线下的所有设备,这些设备通过链表链接起来;driver集合包含了挂在该总线下的所有驱动程序,这些驱动程序通过链表链接起来。
1.总线bus
总线是处理器和设备之间的通道,在设备模型中,所有的设备都通过总线相连,甚至是内部的虚拟“platform”总线。在Linux设备模型中,总线由bus_type结构表示,定义在<linux/device.h>
struct bus_type {
const char *name;/*总线名称*/
const char *dev_name;//用于subsystems枚举设备
struct device *dev_root;//默认的父设备
struct bus_attribute *bus_attrs;/*总线属性*/
struct device_attribute *dev_attrs;//设备属性
struct driver_attribute *drv_attrs;//驱动属性
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 iommu_ops *iommu_ops;
struct subsys_private *p;
};
总线注册/删除
总线的注册使用:bus_register(struct bus_type * bus) 若成功,新的总线将被添加进系统,并可在sysfs 的/sys/bus 下看到。
总线的删除使用:void bus_unregister(struct bus_type *bus)
总线方法
int (*match)(struct device * dev,struct device_driver * drv) 当一个新设备或者驱动被添加到这个总线时,该方法被调用。用于判断指定的驱动程序是否能处理指定的设备。若可以,则返回非零值。
int (*uevent)(struct device *dev,char **envp,int num_envp,char *buffer,int buffer_size) 在为用户空间产生热插拔事件之前,这个方法允许总线添加环境变量。
总线属性
总线属性由结构bus_attribute 描述,定义如下:
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);
};
属性方法:
int bus_create_file(struct bus_type *bus,struct bus_attribute *attr) 创建属性
void bus_remove_file(struct bus_type*bus,struct bus_attribute *attr) 删除属性
2.关于设备device
struct device {
struct device *parent;/*设备的"父"设备,该设备所属的设备,通常一个父设
备是某种总线或者主控制器。如果parent 是NULL, 则该设备是顶层设备,较少见*/
struct device_private *p;/*该设备驱动使用的私有数据成员*/
struct kobject kobj;/*内嵌的kobject*/
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 */
struct dev_pm_info power;
struct dev_pm_domain *pm_domain;
#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; /* dma pools (if dma'ble) */
struct dma_coherent_mem *dma_mem; /* internal for coherent mem
override */
#ifdef CONFIG_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 */
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;
};
注册设备:int device_register(sruct device *dev)
注销设备:void device_unregister(struct device *dev);
设备属性
设备属性由struct device_attribute 描述:
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);
};
创建属性:int device_create_file(struct device*device, struct device_attribute * entry)
删除属性:void device_remove_file(struct device *dev, struct device_attribute * attr)
3.关于驱动driver
struct device_driver {
const char *name;/*驱动程序的名字( 体现在sysfs 中)*/
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 */
const struct of_device_id *of_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;
};
驱动注册/注销
int driver_register(struct device_driver *drv) 注册驱动
void driver_unregister(struct device_driver *drv) 注销驱动
驱动的属性使用struct driver_attribute 来描述:
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);
};
创建属性int driver_create_file(struct device_driver * drv,struct driver_attribute * attr)
删除属性:void driver_remove_file(struct device_driver * drv,struct driver_attribute * attr)