Linux 设备模型浅析之设备篇
本文属本人原创,欢迎转载,转载请注明出处。由于个人的见识和能力有限,不可能面
面俱到,也可能存在谬误,敬请网友指出,本人的邮箱是yzq.seen@,博客是
http:// 。
Linux 设备模型,仅仅看理论介绍,比如LDD3 的第十四章,会感觉太抽象不易理解,而
通过阅读内核代码就更具体更易理解,所以结合理论介绍和内核代码阅读能够更快速的理解掌
握linux 设备模型。这一序列的文章的目的就是在于此,看这些文章之前最好能够仔细阅读
LDD3 的第十四章。大部分device driver 都被包含在一个特定bus 中,platform_device
platform_driver 就是如此,包含在 platform_bus_type 中。这里就以对platform_bus_type 的调用
为主线,浅析platform_device 的注册过程,从而理解linux 设备模型。platform_bus_type 用于关
联SOC 的 platform device platform driver ,比如在内核linux-2.6.2 中所有S3C2410 中的
platform device 都保存在devs.c 中。这里就以S3C2410 RTC 为例。在文章的最后贴有一张针对
本例的device model 图片,可在阅读本文章的时候作为参照。
一、S3C2410 RTC 的platform device 定义在arch/arm/plat-s3c24xx/devs.c 中,如下:
static struct resource s3c_rtc_resource[] = {
[0] = {
.start = S3C24XX_PA_RTC,
.end = S3C24XX_PA_RTC + 0xff,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_RTC,
.end = IRQ_RTC,
.flags = IORESOURCE_IRQ,
},
[2] = {
.start = IRQ_TICK,
.end = IRQ_TICK,
.flags = IORESOURCE_IRQ
}
};
struct platform_device s3c_device_rtc = {
.name = "s3c2410-rtc",
.id = -1,
.num_resources = ARRAY_SIZE(s3c_rtc_resource),
.resource = s3c_rtc_resource,
};
把它们添加在arch/arm/mach-s3c2440/ mach- smdk2440.c 中,如下:
static struct platform_device *smdk2440_devices[] __initdata = {
&s3c_device_usb,
&s3c_device_lcd,
&s3c_device_wdt,
&s3c_device_i2c0,
&s3c_device_iis,
& s3c_device_rtc
};
系统初始化的时候会调用drivers/base/platform.c 里的platform_add_devices(smdk2440_devices,
ARRAY_SIZE(smdk2440_devices))将其注册到platform_bus_type ,最终被添加到device hierarchy
。platform_add_devices()调用了platform_device_register() ,而后者又先后调用了
device_initialize() platform_device_add() 。有必要对pla