of_device_id

of_device_id 用于device和driver的match,其在kernel\linux-3.10.y\include\linux\mod_devicetable.h中定义

/* Struct used for matching a device */
struct of_device_id
{
 char name[32];
 char type[32];
 char compatible[128]; //用于device和driver的match
 const void *data;
};


const struct of_device_id *of_match_device(const struct of_device_id *matches,
        const struct device *dev)
{
 if ((!matches) || (!dev->of_node))
  return NULL;
 return of_match_node(matches, dev->of_node);
}


const struct of_device_id *of_match_node(const struct of_device_id *matches,
      const struct device_node *node)
{
 const struct of_device_id *match;
 unsigned long flags;

 raw_spin_lock_irqsave(&devtree_lock, flags);
 match = __of_match_node(matches, node);
 raw_spin_unlock_irqrestore(&devtree_lock, flags);
 return match;
}


static
const struct of_device_id *__of_match_node(const struct of_device_id *matches,
        const struct device_node *node)
{
 if (!matches)
  return NULL;

 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  int match = 1;
  if (matches->name[0])
   match &= node->name
    && !strcmp(matches->name, node->name);
  if (matches->type[0])
   match &= node->type
    && !strcmp(matches->type, node->type);
  if (matches->compatible[0])
   match &= __of_device_is_compatible(node,
          matches->compatible);
  if (match)
   return matches;
  matches++;
 }
 return NULL;
}


/** Checks if the given "compat" string matches one of the strings in
 * the device's "compatible" property
 */
static int __of_device_is_compatible(const struct device_node *device,
         const char *compat)
{
 const char* cp;
 int cplen, l;

 cp = __of_get_property(device, "compatible", &cplen);
 if (cp == NULL)
  return 0;
 while (cplen > 0) {
  if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
   return 1;
  l = strlen(cp) + 1;
  cp += l;
  cplen -= l;
 }

 return 0;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`of_device_id`是Linux内核中用于匹配设备树节点和驱动程序的结构体。该结构体包含了设备树节点的一些属性信息,如设备类型、厂商信息、设备名称、设备ID等,以及一个与之对应的驱动程序名称或ID,用于在设备树中查找匹配的节点。 以下是`of_device_id`结构体的定义: ```c struct of_device_id { char name[32]; // 设备名称 char type[32]; // 设备类型 const void *data; // 与设备匹配的驱动程序信息 }; ``` `of_device_id`结构体通常作为驱动程序的一部分,在驱动程序初始化时被使用。驱动程序会调用`of_match_device()`函数来根据`of_device_id`结构体中的信息,在设备树中查找匹配的节点,并将其与驱动程序绑定。在匹配成功后,驱动程序可以通过`of_device`结构体获得这个节点的一些属性信息。 例如,以下是一个`of_device_id`结构体的示例: ```c static const struct of_device_id my_driver_of_match[] = { { .compatible = "my-device" }, { /* end of list */ }, }; ``` 在驱动程序初始化时,可以使用`MODULE_DEVICE_TABLE(of, my_driver_of_match)`宏将该`of_device_id`结构体与驱动程序关联起来。然后,内核会自动在设备树中查找`compatible`属性为`"my-device"`的节点,并将其与驱动程序绑定。 总之,`of_device_id`结构体是Linux内核中用于匹配设备树节点和驱动程序的重要机制,它可以让驱动程序自动识别和绑定设备树节点,并简化驱动程序的开发。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值