《电子技术应用》
您所在的位置:首页 > 嵌入式技术 > 其他 > Linux设备驱动程序架构分析之MMC/SD(四)

Linux设备驱动程序架构分析之MMC/SD(四)

2015-04-29
关键词: platform_get_device_id

1630行,调用platform_get_device_id宏,取得处理器类型,该宏定义在include/linux/platform_device.h文件中:

捕获.PNG

但是,回忆一下我们注册的platform_device s3c_device_sdi,我们并没有初始化platform_device.id_entry成员,那么这里的 platform_get_device_id宏返回值是NULL吗?如果不是NULL,应该是什么值呢?

答案是platform_get_device_id(pdev)->driver_data返回值为1。

原因是s3cmci_driver.id_table被设置为s3cmci_driver_ids。s3cmci_driver_ids定义在drivers/mmc/host/s3cmci.c文件中:

捕获.PNG

struct platform_device_id定义在include/linux/mod_devicetable.h文件中:

捕获.PNG

platform_driver.id_table是platform_driver所支持的设备列表。可以看到,s3cmci_driver支持 3种设备,分别是"s3c2410-sdi"、"s3c2412-sdi"和"s3c2440-sdi"。对于"s3c2410-sdi",其 driver_data成员值为0,对于其它两种设备,它们的driver_data成员值为1。

当调用platform_driver_register函数注册s3cmci_driver 时,s3cmci_driver.driver.bus被设置为 platform_bus_type,structbus_type platform_bus_type定义在drivers/base/platform.c文件中:

捕获.PNG

根据《Linux设备模型分析之device_driver(基于3.10.1内核)》 一文对Linux设备模型的分析,在s3cmci_driver.probe函数被调用执行之前,platform_bus_type.match即 platform_match首先会被调用执行。platform_match函数定义在drivers/base/platform.c文件中:

捕获.PNG

737行,如果pdrv->id_table不为空,则调用platform_match_id函数。而我们的 platform_drivers3cmci_driver.id_table被设置为s3cmci_driver_ids,所以 platform_match_id函数会被执行。

platform_match_id函数定义在drivers/base/platform.c文件中:

捕获.PNG

可以看到,在701行,如果platform_device.name与platform_device_id.name相同,则将id赋值给pdev->id_entry。

回到我们的platform_driver s3cmci_driver和platform_device s3c_device_sdi,s3c_device_sdi.name被初始化为"s3c2410-sdi",但是因为我们基于的平台是 Mini2440,处理器是S3C2440,所以s3c244x_map_io函数会被调用,至于什么时候该函数会被调用我还没有搞清楚,呵呵!该函数定 义在arch/arm/mach-s3c24xx/s3c244x.c文件中:

捕获.PNG

71行,将s3c_device_sdi.name设置为"s3c2440-sdi"

而s3cmci_driver.id_table被设置为 s3cmci_driver_ids,s3cmci_driver_ids[2].name同样为"s3c2440-sdi",所以,虽然 s3c_device_sdi.id_entry在初始化时没有赋值,但是在platform_match_id函数中,它会被赋值为 s3cmci_driver_ids[2]。

现在我们可以回到s3cmci_probe函数了:

1630行,经过前面的分析,我们知道platform_get_device_id(pdev)->driver_data的值其实就是 s3cmci_driver_ids[2].driver_data,其值为1。所以,对于Mini2440平台,is2440变量被赋值为1。

1632行,调用mmc_alloc_host函数为struct mmc_host指针变量mmc分配内存空间并初始化。注意mmc_alloc_host的第一个参数表示除了mmc_host结构体外,还要额外多分配 的内存空间大小,所以这里分配的内存大小是struct mmc_host加上struct s3cmci_host。

mmc_alloc_host函数定义在drivers/mmc/core/host.c文件中,其内容如下:

捕获.PNG

捕获.PNG

本站内容除特别声明的原创文章之外,转载内容只为传递更多信息,并不代表本网站赞同其观点。转载的所有的文章、图片、音/视频文件等资料的版权归版权所有权人所有。本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如涉及作品内容、版权和其它问题,请及时通过电子邮件或电话通知我们,以便迅速采取适当措施,避免给双方造成不必要的经济损失。联系电话:010-82306118;邮箱:aet@chinaaet.com。