> For the complete documentation index, see [llms.txt](https://freebsd-journal-cn.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://freebsd-journal-cn.bsdcn.org/20160102-xing-neng-tiao-you/book-review-freebsd-device-drivers.md).

# 书评：《FreeBSD 设备驱动程序开发》——写给无畏者的指南

* 原文：[Book Review: FreeBSD Device Drivers — A Guide for the Intrepid](https://freebsdfoundation.org/our-work/journal/browser-based-edition/performance-tuning/)
* 作者：**Simon Gerraty**

《FreeBSD 设备驱动程序开发》

* **出版社：** No Starch Press，2012 年 5 月
* **印刷版定价：** $49.95
* **电子版定价：** $24.99
* **ISBN：** 978-1-59327-204-3
* **页数：** 352

设备驱动程序是让操作系统能够与硬件打交道的代码。与内核中其它大部分代码不同，它们往往由更熟悉硬件本身而非某个特定操作系统的人维护。

我想我应该算是这本书的目标读者。我学习过几种操作系统的内部原理，移植并修改过一些设备驱动程序，但从 CP/M 时代和 Z80 之后，我就几乎没再和硬件打过交道。不过我至少可以确认一件事：晶体管一旦冒了烟，就再也回不来了。

《FreeBSD 设备驱动程序开发》涵盖了 FreeBSD 中对设备驱动程序至关重要的基础设施。因此，对于希望为 FreeBSD 编写或移植驱动程序的人来说，这本书应当非常有用。

内容编排循序渐进，从每个设备驱动程序都需要的基础骨架讲起，再到资源分配和线程同步。书中涵盖了驱动程序认领所识别硬件的自动配置过程，介绍了用于存储设备的通用访问方法（Common Access Method，CAM）、DMA、USB，最后讲到网络适配器及其与网络栈的交互。

我觉得最有趣的几章是那些专门讨论驱动程序相关 API 的内容。有少数几个小节，我感觉再多深入一些可能会更好。DMA 相关的 API 数量繁多，显然没有多少驱动会全部用到，但展示一些真实示例可能会更有帮助。

作者前言中明确表示，假定读者对操作系统内部机制有一定了解，甚至还给出了推荐的预备阅读材料。为契合目标读者群，本书仅从 API 层面向读者介绍内核向设备驱动程序提供的服务，只有在有助于理解 API 时，才会讨论其背后的运作机制。

许多驱动程序都会包含完成同样任务的代码，后面的章节就跳过了那些前面已经充分讲过的内容。这一点我很欣赏。我很反感许多技术书籍用冗余信息硬凑篇幅——最糟糕的一类，是给你一百来页有用的内容，然后附上三四百页原封不动照搬的手册页！

书中提供了大量带注释的代码清单，用以解释发生了什么。前几章伪设备（pseudo device，即看起来像设备但没有对应硬件的东西）的小节中也有一些简单示例，但大部分清单都来自真实的设备驱动程序。对泛读的读者而言，这种叙述方式可能略显简练，但目标读者应当会认可。我觉得效果相当不错。

USB 这东西很古怪（多半是委员会设计的），我原本了解甚少，但很感兴趣想学一学。本章讲的 ulpt 驱动程序是个不错的示例，但 USB 能用于五花八门的设备，再多一个示例或许会更有用。

总而言之，我认为这本书就其既定目标而言拿捏得相当平衡。它大体走的是“只讲事实”的风格，效果不错，同时为希望深入了解细节或背景的读者提供了大量参考。

如果你需要或希望了解 FreeBSD 设备驱动程序，这本书绝对值得一读。

***

**Simon Gerraty** 是 Juniper Networks 的杰出工程师，自 2000 年起参与 Junos（基于 FreeBSD 的操作系统）的开发。他是 NetBSD 和 FreeBSD 的提交者，自 1993 年起维护 bmake。自 1984 年左右开始，他使用过从个人电脑到主机的各种 UNIX 系统。


---

# 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://freebsd-journal-cn.bsdcn.org/20160102-xing-neng-tiao-you/book-review-freebsd-device-drivers.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.
