驱动设备模型之总线设备驱动

本文详细介绍了Linux设备模型的三大核心组成部分:总线(bus)、设备(device)和驱动(driver)的相关结构体及其操作方法,并通过具体实例展示了它们之间的交互关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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),如下:

View Code

设备属性由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描述,代码如下:

View Code

驱动属性使用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

View Code

device.c 在sysfs/bus/my_bus/devices目录下创建一个设备my_dev

这里my_dev是一个连接文件,它连接到sysfs/devices/my_bus0/my_dev

View Code

driver.c   当加载驱动时候,会在总线上找到它能够处理的设备

View Code

注意:无论是先加载驱动还是设备,都会调用probe这个函数。好比先插u盘,在启动系统 与启动系统后再插u盘,系统一样能够支持。

转载于:https://www.cnblogs.com/zhao-anan/p/6909813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值