"of_match_ptr()" 在设备驱动程序中有什么用

what is the use of "of_match_ptr()" in device drivers

在我做设备驱动的过程中,我了解到of_match_table()是指向兼容性字符串的元组。

但是,我找不到of_match_ptr()的重要性。

of_match_ptr()是在include/linux/of.h中定义的预处理器宏:

#ifdef CONFIG_OF
#define of_match_ptr(_ptr)    (_ptr)
#else /* CONFIG_OF */
#define of_match_ptr(_ptr)    NULL
#endif /* CONFIG_OF */

当使用设备树和开放固件支持编译内核时,即 CONFIG_OF=y 它将导致传递的指针。否则它将产生 NULL。

使用宏而不是直接传递指针,是为了保护of_match_table成员。有些总线驱动程序会检查 of_match_table 是否为 NULL 以不取消引用无效指针。

然而,许多驱动程序是在树内编译的,在其相应的 Kconfig 中定义了一个编译开关,该开关已经依赖于 CONFIG_OF。例如模拟设备ADV748X视频解码器驱动的编译开关has this dependency:

config VIDEO_ADV748X
    tristate "Analog Devices ADV748x decoder"
    depends on VIDEO_V4L2 && I2C
    depends on OF
    select MEDIA_CONTROLLER
    select VIDEO_V4L2_SUBDEV_API
    select REGMAP_I2C
    select V4L2_FWNODE
    help
      V4L2 subdevice driver for the Analog Devices
      ADV7481 and ADV7482 HDMI/Analog video decoders.

      To compile this driver as a module, choose M here: the
      module will be called adv748x.

并省略了 drivers/media/i2c/adv748x/adv748x-core.c 中的宏:

static struct i2c_driver adv748x_driver = {
    .driver = {
        .name = "adv748x",
        .of_match_table = adv748x_of_table,
        .pm = &adv748x_pm_ops,
    },
    .probe_new = adv748x_probe,
    .remove = adv748x_remove,
};