> 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/man4/devctl.4.md).

# devctl.4

`devctl` — 设备事件报告与设备控制接口

## 名称

`devctl`

## 概要

`devctl 驱动会自动包含在内核中。`

## 描述

`devctl` 设备用于从内核报告设备事件。未来版本还将允许进行某些设备控制。

## 实现说明

此设计只允许 **/dev/devctl** 有一个读取者。从长远来看这并不理想，但能从本实现中去掉许多复杂之处。也许我们应该将此设备设为可克隆设备。

还需注意：我们特意不将设备附加到 `device_t` 树中，以避免潜在的鸡生蛋蛋生鸡问题。有人可能会争辩说这一切都属于根节点。还可以进一步争辩说，我们现在拥有的 sysctl(3) 接口可能更适合作为 ioctl(2) 接口。

驱动中包含 `SIGIO` 支持。但是，作者并不确定 `SIGIO` 支持是否正确实现。它是从一个具有 `SIGIO` 支持的驱动中复制的，而该驱动的 `SIGIO` 支持可能自 FreeBSD 3.4 或 FreeBSD 2.2.8 以来就未经测试！

此设备的读取通道用于实时向用户空间报告更改。我们一次返回一条记录。如果你尝试逐字符读取此设备，将丢失其余数据。监听程序需要自行处理这种情况。

sysctl `hw.bus.devctl_queue` 可用于控制队列长度。当没有 devd(8) 运行时，可将其设为 0 以禁用 `devctl`。

## 协议

`devctl` 设备使用 ASCII 协议。驱动一次向其读取者返回一条记录。每条记录以换行符终止。记录的第一个字符是事件类型。

| 类型 | 描述            |
| -- | ------------- |
| !  | 通知事件，如链路状态变化。 |
| +  | 设备树中的设备节点已附加。 |
| -  | 设备树中的设备节点已分离。 |
| ?  | 检测到未知设备。      |

### 消息格式

除记录中的第一个字符外，附加和分离消息具有相同的格式。

> `T``dev` `at` `parent` `on` `location`

| 部分         | 描述             |
| ---------- | -------------- |
| `T`        | + 或 -          |
| `dev`      | 被附加/分离的设备名称。   |
| `parent`   | 附加该设备的父总线设备名称。 |
| `location` | 总线特定的位置信息。     |

nomatch 消息可用于加载设备驱动。如果加载了一个设备驱动，则可能发生两种情况之一。如果该设备驱动附加到了某个设备，你将收到设备附加消息。如果没有，则什么也不会发生。

附加和分离消息在事件发生后到达。这意味着不能使用附加消息来加载替代驱动。附加消息所属的驱动已经占用了此设备。也不能使用分离消息将数据刷新到设备。设备已经不存在了。

所有返回的值均采用"key=value"或"key="value""的形式。对于后者，字符串"value"中的任何内部反斜杠必须加倍。任何内部双引号字符也必须以反斜杠作为前缀。所有其他字符应原样传递。

## 参见

devd(8)


---

# 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/man4/devctl.4.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.
