linux 驱动设备和 dts 匹配过程
linux 设备驱动文件在与 dts 中的设备板级硬件信息匹配的关键字是 compatible 属性。即比较驱动文件中 of_device_id 结构体元素的 .compatible 成员变量和 dts 文件中 node 中 compatible 属性两个字符串。
Rationale:
linux 启动从 lk jump 到 kernel 之后
函数调用的深度比较深所以图比较长,其中细节部分省略,可以打开具体源码去看。
可以看到最后调用的函数
static inline int of_driver_match_device(struct device *dev,const struct device_driver *drv) --->of_match_device(drv->of_match_table, dev) != NULL; --->of_match_node(matches, dev->of_node) --->__of_match_node(matches, node) --->static const struct of_device_id *__of_match_node(const struct of_device_id *matches, const struct device_node *node) { const struct of_device_id *best_match = NULL; int score, best_score = 0; if (!matches) return NULL; for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) { //此函数将driver的of_match_table->compatible和node中的compatible比较 score = __of_device_is_compatible(node, matches->compatible, matches->type, matches->name); if (score > best_score) { best_match = matches; best_score = score; } } return best_match; }
传递到最后__of_device_is_compatible函数将driver的of_match_table->compatible和node中的compatible比较,这个比较不是单纯的比较,是一种加分制。
匹配成功之后会进行probe,如果driver 的 probe 执行不成功(比如硬件问题,或者没有挂载设备),会调用sys系列函数进行驱动卸载。
原文链接:https://blog.csdn.net/sinat_30545941/article/details/85943787
作者:柒月
Q群 :2122210(嵌入式/机器学习)