> 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/dev_module.9.md).

# DEV\_MODULE.9

`DEV_MODULE` — 设备驱动模块声明宏

## 名称

`DEV_MODULE`

## 概要

```c
#include <sys/param.h>

#include <sys/kernel.h>

#include <sys/module.h>

#include <sys/conf.h>

DEV_MODULE(name, modeventhand_t evh, void *arg)
```

## 描述

`DEV_MODULE` 宏声明一个设备驱动内核模块。它填充一个 `moduledata_t` 结构，然后使用正确的参数调用 `DECLARE_MODULE`，其中 `name` 是模块的名称，`evh`（及其参数 `arg`）是模块的事件处理程序（更多信息参见 [DECLARE\_MODULE(9)](/man/man9/declare_module.9.md)）。事件处理程序应在加载时通过 `make_dev` 创建设备，并在卸载时使用 `destroy_dev` 销毁它。

## 实例

```c
#include <sys/module.h>
#include <sys/conf.h>
static struct cdevsw foo_devsw = { ... };
static struct cdev *sdev;
static int
foo_load(module_t mod, int cmd, void *arg)
{
    int err = 0;
    switch (cmd) {
    case MOD_LOAD:
        sdev = make_dev(&foo_devsw, 0, UID_ROOT, GID_WHEEL, 0600, "foo");
        break;          /* 成功*/
    case MOD_UNLOAD:
    case MOD_SHUTDOWN:
        destroy_dev(sdev);
        break;          /* 成功*/
    default:
        err = EINVAL;
        break;
    }
    return(err);
}
DEV_MODULE(foo, foo_load, NULL);
```

## 参见

[DECLARE\_MODULE(9)](/man/man9/declare_module.9.md), destroy\_dev(9), [make\_dev(9)](/man/man9/make_dev.9.md), [module(9)](/man/man9/module.9.md)

## 作者

本手册页由 Alexander Langer <alex@FreeBSD.org> 编写。


---

# 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/dev_module.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.
