linux kernel 驱动模型中匹配顺序是怎样的

今天看驱动,发现有几处都可以进行匹配,我就在想他们的优先顺序是怎样的

这里看的是platform架构

其他的应该一样

跟踪代码发现如下代码:

__driver_attach    //./drivers/base/dd.c

        driver_match_device    ./drivers/base/base.h

                drv->bus->match(dev, drv)

可见,他是找bus上的match函数

platform模型就是在./drivers/base/platform.c

-    1163 struct bus_type platform_bus_type = {
|    1164     .name       = "platform",
|    1165     .dev_groups = platform_dev_groups,
|    1166     .match      = platform_match,
|    1167     .uevent     = platform_uevent,
|    1168     .pm     = &platform_dev_pm_ops,
|    1169 };
     1170 EXPORT_SYMBOL_GPL(platform_bus_type);

用的是platform_match:            

      988 static int platform_match(struct device *dev, struct device_driver *drv)
-     989 {
|     990     struct platform_device *pdev = to_platform_device(dev);
|     991     struct platform_driver *pdrv = to_platform_driver(drv);
|     992
|     993     /* When driver_override is set, only bind to the matching driver */
|     994     if (pdev->driver_override)
|     995         return !strcmp(pdev->driver_override, drv->name);
|     996
|     997     /* Attempt an OF style match first */
|     998     if (of_driver_match_device(dev, drv))
|     999         return 1;
|    1000
|    1001     /* Then try ACPI style match */
|    1002     if (acpi_driver_match_device(dev, drv))
|    1003         return 1;
|    1004
|    1005     /* Then try to match against the id table */
|    1006     if (pdrv->id_table)
|    1007         return platform_match_id(pdrv->id_table, pdev) != NULL;
|    1008
|    1009     /* fall-back to driver name match */
|    1010     return (strcmp(pdev->name, drv->name) == 0);
|    1011 }

这里就能看到它的优先级了:

1、of_driver_match_device

2、pdrv->id_table

3、strcmp(pdev->name, drv->name)

比如如下的驱动:

     928 #ifdef CONFIG_OF
-    929 static const struct of_device_id gmainaf_of_device_id[] = {
|    930     {.compatible = "mediatek,camera_af_lens",},
|    931     {}
|    932 };
     933 #endif
     934
     935 /* platform structure */
-    936 static struct platform_driver g_stAF_Driver = {
|    937     .probe = AF_probe,
|    938     .remove = AF_remove,
|    939     .suspend = AF_suspend,
|    940     .resume = AF_resume,
|-   941     .driver = {
||   942         .name = PLATFORM_DRIVER_NAME,
||   943         .owner = THIS_MODULE,
||   944 #ifdef CONFIG_OF
||   945         .of_match_table = gmainaf_of_device_id,
||   946 #endif
||   947     } ,
            .id_table = match_id,
            
        };
     948
-    949 static struct platform_device g_stAF_device = {
|    950     .name = PLATFORM_DRIVER_NAME, .id = 0, .dev = {} };
     951
     952 static int __init MAINAF_i2C_init(void)
-    953 {
|    954 #if I2C_CONFIG_SETTING == 1
|    955     i2c_register_board_info(LENS_I2C_BUSNUM, &kd_lens_dev, 1);
|    956 #endif
|    957
|-   958     if (platform_device_register(&g_stAF_device)) {
||   959         LOG_INF("failed to register AF driver\n");
||   960         return -ENODEV;
||   961     }
|    962
|-   963     if (platform_driver_register(&g_stAF_Driver)) {
||   964         LOG_INF("Failed to register AF driver\n");
||   965         return -ENODEV;
||   966     }
|    967
|    968     return 0;
|    969 }

优先级依次是:

1、

.driver = {
||   942         .name = PLATFORM_DRIVER_NAME,
||   943         .owner = THIS_MODULE,
||   944 #ifdef CONFIG_OF
||   945         .of_match_table = gmainaf_of_device_id,
||   946 #endif
||   947     }

也就是匹配dts

2、id_table = match_id

3、platform device的name 与driver的name

platform device的name是这个:

 static struct platform_device g_stAF_device = {
         .name = PLATFORM_DRIVER_NAME, .id = 0, .dev = {} };

driver的name 是这个:

.driver = {
    .name = PLATFORM_DRIVER_NAME,

注意两者有点不同

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值