> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man9/driver.9.md).

# driver.9

`driver` — 描述设备驱动的结构

## 名称

`driver`

## 概要

```c
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
static int foo_probe(device_t);
static int foo_attach(device_t);
static int foo_detach(device_t);
static int foo_frob(device_t, int, int);
static int foo_twiddle(device_t, char *);
static device_method_t foo_methods[] = {
	/* 来自 device 接口的方法 */
	DEVMETHOD(device_probe,		foo_probe),
	DEVMETHOD(device_attach,	foo_attach),
	DEVMETHOD(device_detach,	foo_detach),
	/* 来自 bogo 接口的方法 */
	DEVMETHOD(bogo_frob,		foo_frob),
	DEVMETHOD(bogo_twiddle,		foo_twiddle),
	/* 终止方法列表 */
	DEVMETHOD_END
};
static driver_t foo_driver = {
	"foo",
	foo_methods,
	sizeof(struct foo_softc)
};
DRIVER_MODULE(foo, bogo, foo_driver, NULL, NULL);
```

## 描述

内核中的每个驱动由一个 `driver_t` 结构描述。该结构包含设备名称、指向方法列表的指针、驱动所实现的设备类型指示，以及驱动需要与设备实例关联的私有数据大小。每个驱动会实现一组或多组方法（称为接口）。示例驱动实现了标准的 "driver" 接口和虚构的 "bogo" 接口。

当驱动向系统注册时（通过 `DRIVER_MODULE` 宏，参见 [DRIVER\_MODULE(9)](https://github.com/FreeBSD-Ask/freebsd-man-sc/tree/main/man9/driver_module.9.md)），它会被添加到其父总线类型的 devclass 所包含的驱动列表中。例如，所有 PCI 驱动会包含在名为 "pci" 的 devclass 中，所有 ISA 驱动会包含在名为 "isa" 的 devclass 中。驱动不存放在父总线的设备对象中是为了处理给定总线类型的多个实例。`DRIVER_MODULE` 宏还会创建以驱动名称命名的 devclass，并可通过指定最后两个参数为额外的模块事件处理器和参数，可选地调用驱动中的额外初始化代码。

## 参见

[devclass(9)](/man/man9/devclass.9.md), [device(9)](/man/man9/device.9.md), [DEVICE\_ATTACH(9)](/man/man9/device_attach.9.md), [DEVICE\_DETACH(9)](/man/man9/device_detach.9.md), [DEVICE\_IDENTIFY(9)](https://github.com/FreeBSD-Ask/freebsd-man-sc/tree/main/man9/device_identify.9.md), [DEVICE\_PROBE(9)](/man/man9/device_probe.9.md), [DEVICE\_SHUTDOWN(9)](https://github.com/FreeBSD-Ask/freebsd-man-sc/tree/main/man9/device_shutdown.9.md), [DRIVER\_MODULE(9)](https://github.com/FreeBSD-Ask/freebsd-man-sc/tree/main/man9/driver_module.9.md)

## 历史

`driver` 框架首次出现于 FreeBSD 2.2.7。

## 作者

本手册页由 Doug Rabson 编写。


---

# 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://book.bsdcn.org/man/man9/driver.9.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.
