evice *dev, struct kobj_uevent_env *env); 9 int (*probe)(struct device *dev); 10 int (*remove)(struct device *dev); 11 void (*shutdown)(struct device *dev); 12 13 int (*suspend)(struct device *dev, pm_message_t state); 14 int (*resume)(struct device *dev); 15 16 const struct dev_pm_ops *pm; 17 device 18 struct subsys_private *p; 19 };
bus_type结构体中重要的方法:
match:当一个新设备或驱动被添加到这条总线时,该方法被调用。用来判断制定的驱动程序是否能够处理指定的设备。若可以,返回非零值。
uevent:为用户空间产生热插拔事件之前,这个方法允许总线添加环境变量;
bus_attribute: 总线属性,这个结构体如下
1 struct bus_attribute { 2 struct attribute attr; 3 ssize_t (*show)(struct bus_type *bus, char *buf); 4 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count); 5 };
总线属性的操作方法:
int bus_create_file(struct bus_type *bus,struct bus_attribute *attr) //创建属性
void bus_remove_file(struct bus_type*bus, struct bus_attribute *attr) //删除属性
总线的常用操作函数:
int bus_register(struct bus_type * bus) //总线的注册
void bus_unregister(struct bus_type *bus) //总线的删除
第二节:设备
linux每个设备又结构体devices来定义(位于linux/device.h),如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
设备属性由struct device_attribute描述:
1 struct device_attribute { 2 struct attribute attr; 3 ssize_t (*show)(struct device *dev, struct device_attribute *attr, 4 char *buf); 5 ssize_t (*store)(struct device *dev, struct device_attribute *attr, 6 const char *buf, size_t count); 7 };
设备属性的常用操作方法:
int device_create_file(struct device *device, struct device_attribute * entry) //创建属性
void device_remove_file(struct device * dev, struct device_attribute * attr) //删除属性
设备的常用操作函数:
int device_register(struct device *dev) //注册一个设备;
void device_unregister(struct device *dev) //注销设备
注意:一条总线也是个设备,也必须按设备注册;
第三节:驱动
驱动程序由struct device_driver描述,代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
驱动属性使用struct driver_attribute来描述,如下:
1 struct driver_attribute { 2 struct attribute attr; 3 ssize_t (*show)(struct device_driver *driver, char *buf); 4 ssize_t (*store)(struct device_driver *driver, const char *buf, 5 size_t count); 6 };
驱动属性的常用操作方法:
int driver_create_file(struct device_driver * drv,struct driver_attribute * attr) //创建属性
void driver_remove_file(struct device_driver * drv,struct driver_attribute * attr) /删除属性
驱动常用的操作函数:
int driver_register(struct device_driver *drv) //注册驱动
void driver_unregister(struct device_driver *drv) //注销驱动
实例分析:
至此,已经介绍完设备模型的基本结构,下面以一个实例分析三者之间的关系:
bus.c 在sysfs/bus下创建一个总线目录my_bus
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
device.c 在sysfs/bus/my_bus/devices目录下创建一个设备my_dev
这里my_dev是一个连接文件,它连接到sysfs/devices/my_bus0/my_dev
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
driver.c 当加载驱动时候,会在总线上找到它能够处理的设备
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
注意:无论是先加载驱动还是设备,都会调用probe这个函数。好比先插u盘,在启动系统 与启动系统后再插u盘,系统一样能够支持。