关于platform中的id_table

来源:互联网 发布:淘宝美工助理下载 编辑:程序博客网 时间:2024/06/08 01:30

转载 http://blog.csdn.net/mcgrady_tracy/article/details/38980991

内核版本:Linux-3.4.67

platform的match函数如下:

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * platform_match - bind platform device to platform driver. 
  3.  * @dev: device. 
  4.  * @drv: driver. 
  5.  * 
  6.  * Platform device IDs are assumed to be encoded like this: 
  7.  * "<name><instance>", where <name> is a short description of the type of 
  8.  * device, like "pci" or "floppy", and <instance> is the enumerated 
  9.  * instance of the device, like '0' or '42'.  Driver IDs are simply 
  10.  * "<name>".  So, extract the <name> from the platform_device structure, 
  11.  * and compare it against the name of the driver. Return whether they match 
  12.  * or not. 
  13.  */  
  14. static int platform_match(struct device *dev, struct device_driver *drv)  
  15. {  
  16.     struct platform_device *pdev = to_platform_device(dev);  
  17.     struct platform_driver *pdrv = to_platform_driver(drv);  
  18.   
  19.     /* Attempt an OF style match first */  
  20.     if (of_driver_match_device(dev, drv))  
  21.         return 1;  
  22.   
  23.     /* Then try to match against the id table */  
  24.     if (pdrv->id_table)  
  25.         return platform_match_id(pdrv->id_table, pdev) != NULL;  
  26.   
  27.     /* fall-back to driver name match */  
  28.     return (strcmp(pdev->name, drv->name) == 0);  
  29. }  
从match函数中可以看到,platform总线匹配设备和驱动有两种方法:一是通过platform_driver中的id_table,如果id_table不存在,则只是简单的比较设备中的name字段和驱动中的name字段是否相同。后面一种方法呢,已经见过无数次了,这里也就不再举例了,这里主要是看前一种方法。例如rtc-s3c.c:
[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. static struct platform_device_id s3c_rtc_driver_ids[] = {  
  2.     {  
  3.         .name       = "s3c2410-rtc",  
  4.         .driver_data    = TYPE_S3C2410,  
  5.     }, {  
  6.         .name       = "s3c2416-rtc",  
  7.         .driver_data    = TYPE_S3C2416,  
  8.     }, {  
  9.         .name       = "s3c2443-rtc",  
  10.         .driver_data    = TYPE_S3C2443,  
  11.     }, {  
  12.         .name       = "s3c64xx-rtc",  
  13.         .driver_data    = TYPE_S3C64XX,  
  14.     },  
  15.     { }  
  16. };  
上面就是platform_device_id,可以看到支持的不只一种,包括2410、2416等等。而Platform_driver定义如下:
[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. static struct platform_driver s3c_rtc_driver = {  
  2.     .probe      = s3c_rtc_probe,  
  3.     .remove     = __devexit_p(s3c_rtc_remove),  
  4.     .suspend    = s3c_rtc_suspend,  
  5.     .resume     = s3c_rtc_resume,  
  6.     .id_table   = s3c_rtc_driver_ids,  
  7.     .driver     = {  
  8.         .name   = "s3c-rtc",  
  9.         .owner  = THIS_MODULE,  
  10.         .of_match_table = s3c_rtc_dt_match,  
  11.     },  
  12. };  

2410的platfrom_device定义如下:
[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. struct platform_device s3c_device_rtc = {  
  2.     .name       = "s3c2410-rtc",  
  3.     .id     = -1,  
  4.     .num_resources  = ARRAY_SIZE(s3c_rtc_resource),  
  5.     .resource   = s3c_rtc_resource,  
  6. };  
那么在匹配的时候,match函数会在驱动的id_table表里面查找同platform device的name字段是否相同,如果相同则返回true,否则返回false。那么通过id_table这种方式有什么好处呢,如果只是简单的比较name字段是否相同,那么一个驱动只能支持特定的一个设备,而如果通过id_table的方式呢,一个驱动可以支持很多个设备,而它们只是name字段不同而已。

0 0