platform_driver结构体
时间: 2023-09-16 19:08:32 浏览: 155
platform_driver结构体是Linux内核中用于管理平台设备驱动的结构体,它包含了平台驱动程序的所有信息和操作方法,包括设备的初始化、配置、挂载、卸载等功能。以下是platform_driver结构体的主要成员:
1. driver:指向一个struct device_driver结构体的指针,描述了该驱动程序的所有属性和操作方法。
2. probe:设备探测函数指针,当一个平台设备被发现时,该函数会被调用,用于初始化和配置设备。
3. remove:设备卸载函数指针,当一个平台设备被移除时,该函数会被调用,用于释放设备资源和清理设备状态。
4. suspend:设备挂起函数指针,当系统需要挂起设备时,该函数会被调用,用于保存设备状态和关闭设备。
5. resume:设备恢复函数指针,当系统需要恢复设备时,该函数会被调用,用于恢复设备状态和打开设备。
6. id_table:平台设备ID表,包含了该驱动程序支持的所有平台设备的ID。
7. driver.name:驱动程序的名称。
8. driver.owner:驱动程序的所有者。
9. driver.bus:驱动程序所属的总线类型。
10. driver.of_match_table:设备树匹配表,用于匹配设备树中的平台设备。
11. driver.acpi_match_table:ACPI匹配表,用于匹配ACPI设备。
12. driver.pm:设备的电源管理,用于控制设备的电源状态。
13. driver.probe_type:设备探测类型,用于指定设备探测的方式。
相关问题
platform_device结构体 platform_driver结构体 device_driver结构体的关系
<think>我们正在讨论Linux设备模型中的三个关键结构体:platform_device、platform_driver和device_driver。
它们之间的关系是:
1. platform_device 代表一个具体的平台设备,通常描述SoC上的外设(如I2C控制器、SPI控制器等),它包含设备的资源(如内存地址、中断号等)。
2. device_driver 是一个通用的设备驱动结构体,包含驱动的基本操作(如probe、remove)和设备模型需要的字段。
3. platform_driver 是平台设备驱动,它内嵌了一个device_driver结构体,并扩展了平台设备特有的操作(如平台设备匹配和电源管理)。
具体关系如下:
- platform_driver 结构体中包含一个 device_driver 结构体成员(通常命名为driver)。
- platform_driver 通过其内部的 device_driver 成员与内核设备模型关联。
- 当注册一个 platform_driver 时,实际上是通过其内部的 device_driver 注册到总线上(platform_bus_type)。
- platform_device 和 platform_driver 通过名称进行匹配(即platform_device的name字段和platform_driver的id_table或者driver.name匹配)。
- 匹配成功后,会调用 platform_driver 中的 probe 函数。
结构体定义(简化):
```c
struct platform_device {
const char *name; // 设备名称,用于匹配驱动
int id;
struct device dev; // 内嵌的通用设备结构体
// ... 资源信息等
};
struct device_driver {
const char *name; // 驱动名称
struct bus_type *bus; // 所属总线
int (*probe)(struct device *dev); // 探测设备
int (*remove)(struct device *dev); // 移除设备
// ... 其他操作
};
struct platform_driver {
int (*probe)(struct platform_device *); // 平台设备的探测函数
int (*remove)(struct platform_device *); // 移除函数
struct device_driver driver; // 内嵌的通用设备驱动
const struct platform_device_id *id_table; // 设备匹配表
};
```
工作流程:
1. 系统启动时,平台设备(platform_device)通常通过板级文件或设备树被注册到内核。
2. 驱动程序通过调用`platform_driver_register`注册一个platform_driver。
3. 内核的platform总线(platform_bus_type)会尝试将已注册的设备与驱动进行匹配。
4. 匹配依据:platform_driver的driver.name与platform_device的name匹配,或者platform_driver的id_table中的任一条目与platform_device的name匹配。
5. 匹配成功则调用platform_driver的probe函数(注意:实际调用的是platform_driver->driver.probe,但通常这个函数指针被设置为一个内部函数,该内部函数会调用platform_driver->probe,并将device转换为platform_device)。
注意:在platform_driver的初始化中,通常会将device_driver的probe函数设置为一个通用的平台驱动probe函数(例如platform_drv_probe),这个函数会调用platform_driver自己的probe函数。
总结:
- platform_device 描述硬件设备。
- platform_driver 包含驱动该硬件设备的函数,并内嵌一个device_driver。
- device_driver 是设备驱动的通用部分,由platform_driver中的driver成员实现。
关系图:
platform_driver -> 包含 device_driver
platform_device -> 包含 device
在总线上,device_driver和device进行匹配,而实际上匹配的是platform_device和platform_driver。
§§相关问题§§
1. 在内核中,platform_device和platform_driver是如何通过名称进行匹配的?
2. 当platform_driver注册时,其内嵌的device_driver的probe函数是如何被设置并最终调用到platform_driver的probe函数的?
3. 设备树(Device Tree)机制下,platform_device是如何被创建和注册的?
4. platform_device结构体中的struct device dev成员与device_driver结构体有何关联?
5. 在platform驱动模型中,为什么需要将device_driver内嵌在platform_driver中,而不是直接使用device_driver?</think>### Linux设备驱动模型核心结构体关系解析
#### 1. **基础层级关系**
```c
struct device_driver { // 通用驱动抽象
const char *name; // 驱动名称
struct bus_type *bus; // 所属总线
int (*probe)(struct device *dev); // 探测设备
...
};
struct platform_driver { // 平台设备驱动
int (*probe)(struct platform_device *);
struct device_driver driver; // 内嵌通用驱动结构
const struct platform_device_id *id_table;
...
};
struct platform_device { // 平台设备实例
const char *name; // 设备名称
struct device dev; // 内嵌设备基类
struct resource *resource; // 设备资源
...
};
```
#### 2. **关键交互机制**
1. **注册绑定流程**
```mermaid
graph LR
A[platform_device_register] --> B[注册到platform_bus]
C[platform_driver_register] --> D[注册driver到platform_bus]
B & D --> E[总线匹配driver.name与device.name]
E --> F[调用platform_driver.probe]
```
2. **结构体内嵌关系**
- `platform_driver` 包含 `device_driver` 成员
- `platform_device` 包含 `device` 成员
- 通过`container_of`宏实现父子结构转换:
```c
struct platform_device *pdev = container_of(dev, struct platform_device, dev);
struct platform_driver *pdrv = container_of(drv, struct platform_driver, driver);
```
#### 3. **功能对比表**
| 结构体 | 作用域 | 关键成员 | 生命周期管理 |
|------------------|-------------|----------------------------|-------------------|
| `device_driver` | 通用驱动模型 | name, bus, probe | 所有设备类型通用 |
| `platform_driver`| 平台设备驱动 | driver, probe, id_table | 封装platform特有操作 |
| `platform_device`| 平台设备实例 | name, dev, resource | 描述SoC固定资源 |
#### 4. **典型驱动开发流程**
```c
// 定义平台驱动
static struct platform_driver my_driver = {
.probe = my_probe,
.driver = {
.name = "my_device", // 匹配设备名称
.owner = THIS_MODULE,
},
};
// 设备探测函数
static int my_probe(struct platform_device *pdev)
{
// 1. 获取设备资源
struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
// 2. 映射寄存器
void __iomem *base = devm_ioremap_resource(&pdev->dev, res);
// 3. 注册字符设备等操作
...
}
// 注册驱动
module_platform_driver(my_driver);
```
#### 5. **设备树(Device Tree)整合**
现代Linux使用设备树替代硬编码`platform_device`:
```dts
my_device: my_device@0x12340000 {
compatible = "vendor,my-device"; // 匹配驱动
reg = <0x12340000 0x1000>; // 寄存器空间
interrupts = <0 45 4>; // 中断号
};
```
驱动通过`of_match_table`匹配:
```c
static const struct of_device_id my_of_ids[] = {
{ .compatible = "vendor,my-device" },
{}
};
struct platform_driver my_driver = {
.driver = {
.of_match_table = my_of_ids,
},
};
```
platform_driver 结构体使用说明
platform_driver 结构体是 Linux 内核中用于设备驱动程序的一种抽象,它允许驱动程序开发者更容易地与 Linux 的 platform device 框架集成。platform_driver 结构体中包含了驱动程序的注册和注销函数,以及与平台设备匹配所需的信息。
一个典型的 platform_driver 结构体定义如下:
```c
#include <linux/platform_device.h>
static int my_driver_probe(struct platform_device *pdev)
{
// 设备探测逻辑
return 0;
}
static int my_driver_remove(struct platform_device *pdev)
{
// 设备移除逻辑
return 0;
}
static const struct of_device_id my_driver_dt_ids[] = {
{ .compatible = "vendor,my-driver", },
{ /* sentinel */ }
};
static struct platform_driver my_driver = {
.probe = my_driver_probe,
.remove = my_driver_remove,
.driver = {
.name = "my_driver",
.owner = THIS_MODULE,
.of_match_table = my_driver_dt_ids,
},
};
module_platform_driver(my_driver);
```
使用说明:
1. 注册驱动:首先定义一个 platform_driver 结构体,其中包含 probe 和 remove 函数指针,这些函数将由内核在设备匹配成功时调用。同时定义一个 driver 成员,它是一个 device_driver 结构体,其中包含 driver_name、owner 和 of_match_table 等成员,用于指定驱动名称、拥有者和设备树兼容性表。
2. 设备匹配:内核使用 of_match_table 中的兼容性字符串来匹配设备树中的 platform_device 设备节点。如果匹配成功,probe 函数将被调用。
3. 探测和移除函数:probe 函数负责初始化驱动和设备,remove 函数负责清理在 probe 期间分配的资源。这些函数会在设备添加或移除时被内核调用。
4. 注销驱动:通过调用 platform_driver_register 函数来注册驱动,或者使用 module_platform_driver 宏来简化注册过程。注销驱动可以通过调用 platform_driver_unregister 宏或者模块卸载来完成。
阅读全文
相关推荐














