Linux设备模型之platform总线.doc
文本预览下载声明
Linux设备模型之platform总线-
一:前言
Platform总线是kernel中最近加入的一种虚拟总线.在近版的2.6kernel中,很多驱动都用platform改写了.只有在分析完platform总线之后,才能继续深入下去分析.在分析完sysfs和设备驱动模型之后,这部份应该很简单了.闲言少叙.步入正题.GO.GO!以下的源代码分析是基于2.6.25的.
二:platform概貌
在分析源代码之前,先在内核代码中找一个platform架构的驱动程序.下面以i8042芯片的驱动为例进行分析.
在linux-2.6.25/drivers/input/serio/i8042.c的intel 8042的初始化入口中,有以下代码分段:
static int __init i8042_init(void)
{
……
err = platform_driver_register(i8042_driver);
if (err)
goto err_platform_exit;
i8042_platform_device = platform_device_alloc(i8042, -1);
if (!i8042_platform_device) {
err = -ENOMEM;
goto err_unregister_driver;
}
err = platform_device_add(i8042_platform_device);
if (err)
goto err_free_device;
……
}
我们在上面的程序片段中看到,驱动程序先注册了一个platform device.然后又添加了一个platform device.这里就涉及到了platform的两个最主要的操作,一个设备驱动注册,一个设备注册.
要了解platform总线的来龙去脉.得从它的初始化开始.
三:platform初始化
Platform总线的初始化是在linux-2.6.25/drivers/base/platform.c中的platform_bus_init()完成的,代码如下:
int __init platform_bus_init(void)
{
int error;
error = device_register(platform_bus);
if (error)
return error;
error = bus_register(platform_bus_type);
if (error)
device_unregister(platform_bus);
return error;
}
上面代码中调用的子函数在linux设备模型深探中已经分析过了.这段初始化代码创建了一个名为 “platform”的设备.后续platform的设备都会以此为parent.在sysfs中表示为:所有platform类型的设备都会添加在platform_bus所代码的目录下.即 /sys/devices/platform下面.
接着,这段初始化代码又创建了一个名为“platform”的总线.
platform_bus_type的定义如下:
struct bus_type platform_bus_type = {
.name = platform,
.dev_attrs = platform_dev_attrs,
.match = platform_match,
.uevent = platform_uevent,
.suspend = platform_suspend,
.suspend_late = platform_suspend_late,
.resume_early = platform_resume_early,
.resume = platform_resume,
};
我们知道,在bus_type中包含了诸如设备与驱动匹配(mach)、hotplug(uevent)事件等很多重要的操作.这些操作在分析platform设备注册与platform驱动注册的时候依次分析.
四:platform device注册
在intel 8042的驱动代码中,我们看到注册一个platform device分为了两部分,一部份是创建一个platform device结构,另一部份
显示全部