1 数据结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 通过 platform_bus_init 函数进行注册
const struct bus_type platform_bus_type = {
.name = "platform",
.dev_groups = platform_dev_groups,
.match = platform_match,
.uevent = platform_uevent,
.probe = platform_probe,
.remove = platform_remove,
.shutdown = platform_shutdown,
.dma_configure = platform_dma_configure,
.dma_cleanup = platform_dma_cleanup,
.pm = &platform_dev_pm_ops,
};

// 通过 platform_add_devices 函数进行注册
struct platform_device {
const char *name; // 2. 再比较能支持的设备,name 和 platform_driver.id_table[i].name
int id;
bool id_auto;
struct device dev;
u64 platform_dma_mask;
struct device_dma_parameters dma_parms;
u32 num_resources;
struct resource *resource; // 指定资源

const struct platform_device_id *id_entry;
/*
* Driver name to force a match. Do not set directly, because core
* frees it. Use driver_set_override() to set or clear it.
*/
const char *driver_override; // 1. 先比较 driver_override 和 platform_driver.driver.name

/* MFD cell pointer */
struct mfd_cell *mfd_cell;

/* arch specific additions */
struct pdev_archdata archdata;
};

// 出了 driver 结构体之外,还定义了一些函数,这些函数和 driver 结构体一样,只不过输入变成了 platform_device
// 使用 platform_driver_register 函数注册.
struct platform_driver {
int (*probe)(struct platform_device *); // 探测函数,如果匹配到了目标设备会自动调用 probe(必须实现)
void (*remove)(struct platform_device *); // 如果配匹配到的设备从总线移除了,会自动调用 remove(必须实现)
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver; // 是 platform_driver 的父类, 3. 再比较 platform_device.name 和 platform_driver.driver.name
// id_table 表示该 drv 能支持多个设备,里面列出了各个 device 的 {.name, .driver_data}
const struct platform_device_id *id_table;
bool prevent_deferred_probe;
/*
* For most device drivers, no need to care about this flag as long as
* all DMAs are handled through the kernel DMA API. For some special
* ones, for example VFIO drivers, they know how to manage the DMA
* themselves and set this flag so that the IOMMU layer will allow them
* to setup and manage their own I/O address space.
*/
bool driver_managed_dma;
};

// 描述设备资源
// 通常使用 platform_get_resource 函数获取资源
struct resource {
resource_size_t start; // 资源启始地址
resource_size_t end; // 资源结束地址
const char *name;
unsigned long flags; // 资源标识位
unsigned long desc;
struct resource *parent, *sibling, *child;
};

2 匹配过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
static int platform_match(struct device *dev, const struct device_driver *drv)
{
struct platform_device *pdev = to_platform_device(dev);
struct platform_driver *pdrv = to_platform_driver(drv);

/* When driver_override is set, only bind to the matching driver */
// 见 1
if (pdev->driver_override)
return !strcmp(pdev->driver_override, drv->name);

/* Attempt an OF style match first */
// 见 2
if (of_driver_match_device(dev, drv))
return 1;

/* Then try ACPI style match */
// 见 3
if (acpi_driver_match_device(dev, drv))
return 1;

/* Then try to match against the id table */
if (pdrv->id_table)
return platform_match_id(pdrv->id_table, pdev) != NULL;

/* fall-back to driver name match */
return (strcmp(pdev->name, drv->name) == 0);
}