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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
struct device platform_bus = {
.init_name = "platform",
};
struct bus_type platform_bus_type = {
.name = "platform",
.dev_attrs=platform_dev_attrs,
.match = platform_match,
.uevent = platform_uevent,
.pm = &platform_dev_pm_ops,
};
stat_kernel()
rest_init()
kernel_thread(kernel_init...)
kernel_init
do_basic_setup()
driver_init()
platform_bus_init()
device_register(&platform_bus) //注册总线设备
bus_register(&platform_bus_type) //注册总线
//至此虚拟总线已经注册成功,可以用它来管理设备和驱动了

struct resources{
resource_size start;//资源起始地址
resource_size end;//资源结束地址
const char *name;
ulong flags; //IO资源还是内存资源还是中断资源
struct resources *parent, *sibling, *child;
}
struct platform_device{
const char *name;
int id;
struct device dev;//描述设备的基类
u32 num_resources;//资源数组长度
struct resources *resources;//资源数组
const struct platform_device_id *id_entry;//某种匹配方式用到的
...
}

struct platform_driver{
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device* );
shutdown,suspend,resume
struct device_driver driver;
const struct platform_device_id *id_table;//某种匹配方式用到的
}

//在你的驱动内初始化struct platform_device *pdev,调用该函数
platform_device_register(struct platform_device *pdev)
platform_device_add(pdev);
pdev->dev.parent = &platform_bus;//设置其父类为
pdev->dev.bus = &platform_bus_type;//设置其所属总线
//至此pdev已经挂到虚拟总线上了
device_add(&pdev->dev); //把pdev注册到内核

//在你的驱动内初始化struct platform_driver *drv,调用该函数
platform_driver_register(struct platform_driver *drv)
drv->driver.bus = &platform_bus_type;//设置其所属总线
drv->driver.probe = platform_drv_probe;
drv->driver.remove = platform_drv_remove;
dirver_register(&drv->dirver);//把drv注册到内核
bus_add_driver(&drv->dirver)
dirver_attach(&drv->dirver)
bus_for_each_dev(drv->bus, NULL, &drv->driver, __driver_attach)//遍历全部dev,执行__driver_attach(dev,&drv->driver)
__driver_attach(dev, drv)
if(!driver_match_device(drv,dev)) return 0; //调用platform_match,失败就返回
driver_probe_device(drv, dev)
really_probe(dev,drv)
dev->driver = drv;
drv->probe(dev) //platform_drv_probe(struct device *_dev)


platform_match(struct device *dev, struct device_driver *drv)
struct platform_device *pdev = to_platform_device(dev);
struct platform_driver *pdrv = to_platform_driver(drv);
if(pdrv->id_table){ return platform_match_id(pdrv->id_table, pdev); }
return (strcmp(pdev->name, drv->name) == 0);

platform_drv_probe(struct device *_dev)
struct platform_driver *drv = to_platform_ddriver(_dev->driver);
struct platform_device *dev = to_platform_device(_dev);
drv->probe(dev);



//驱动代码

int charDrvInit(void)
{
platform_device_register(&s3c_device_lcd);
platform_driver_register(&s3c2410led_driver);
}
void __exit charDrvExit(void)
{
platform_device_unregister(&s3c_device_lcd);
platform_driver_unregister(&s3c2410led_driver);
}