> For the complete documentation index, see [llms.txt](https://richardweiyang-2.gitbook.io/kernel-exploring/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://richardweiyang-2.gitbook.io/kernel-exploring/00-device_model/04-bind.md).

# 绑定

在内核中驱动和设备发生关联称之为绑定bind。

而且有意思的是绑定的过程可以在两个地方发生。

* 驱动加载
* 设备发现

不过这两个地方最后都会通过函数\_\_driver\_attach()来处理。而这个过程又可以分成两步：

* 匹配
* 探测

接着我们就按照这两个步骤展开来看。

## 匹配

匹配的过程由总线的match函数来处理。

```
static inline int driver_match_device(struct device_driver *drv,
				      struct device *dev)
{
	return drv->bus->match ? drv->bus->match(dev, drv) : 1;
}
```

那具体的操作就由总线来决定。比如pci总线上的match函数就是pci\_match\_device()。它的过程就是匹配pci的厂商号和设备号。

## 探测

找到了匹配的设备和驱动后，就可以执行探测了。这个过程相对比较复杂，在当前的内核中，大多最后由总线的probe函数执行。

比如pci总线上的probe函数就是pci\_device\_probe()。它的作用就是去调用pci\_driver中的probe函数。

## 流程图

如果我们简单画出绑定过程的流程，大致如下：

```
  __driver_attach
      driver_match_device()
        bus->match()
      driver_probe_device()
        bus->probe()
```

从这点可以看出，总线的重要性。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://richardweiyang-2.gitbook.io/kernel-exploring/00-device_model/04-bind.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
