编写设备驱动程序

设备树概述

Solaris OS 中的设备表示为互连的设备信息节点树。设备树描述特定计算机的已装入设备的配置。

设备树组件

系统将会生成树结构,其中包含有关引导时连接到计算机的设备的信息。此外,系统正常运行时也可以动态重新配置设备树。设备树从表示平台的根设备节点开始。

根节点下面是设备树的分支。分支由一个或多个总线结点设备和一个终止叶设备组成。

总线结点设备可为设备树中的从属设备提供总线映射和转换服务。PCI - PCI 网桥、PCMCIA 适配器和 SCSI HBA 都是结点设备的示例。编写结点设备驱动程序的讨论仅限于 SCSI HBA 驱动程序的开发(请参见第 18 章)。

叶设备通常为外围设备,如磁盘、磁带、网络适配器、帧缓存器等。叶设备驱动程序可以导出传统的字符驱动程序接口和块驱动程序接口。通过这些接口,用户进程可在存储设备或通信设备中读取和写入数据。

系统通过以下步骤来生成树:

  1. CPU 经过初始化后搜索固件。

  2. 主要固件(OpenBoot、基本输入/输出系统 (Basic Input/Output System, BIOS) 或 Bootconf)初始化并创建包含已知或自标识硬件的设备树。

  3. 当主要固件在设备中发现兼容固件时,主要固件将初始化该设备并检索设备属性。

  4. 该固件将查找并引导操作系统。

  5. 内核从树的根节点开始,搜索匹配的设备驱动程序并将该驱动程序绑定到设备。

  6. 如果设备是结点,则内核会查找固件尚未检测到的子设备。内核会将所有子设备都添加到树的子树节点下面。

  7. 内核从步骤 5 开始重复该过程,直到无需再创建设备节点。

每个驱动程序都会导出设备操作结构 dev_ops(9S),以定义设备驱动程序可以执行的操作。设备操作结构包含通用操作(如 attach(9E)detach(9E)getinfo(9E))的函数指针。该结构同时还包含了一组与特定总线结点驱动程序操作相关的函数指针,以及一组与特定叶结点设备驱动程序操作相关的函数指针。

树结构将在节点之间创建父子关系。此父子关系是体系结构无关性的关键。当叶驱动程序或总线结点驱动程序本质上需要依赖于体系结构的服务时,该驱动程序会请求其父级提供该服务。采用此方法,不管计算机或处理器的体系结构是什么,驱动程序都可以正常运行。下图显示了典型的设备树。

图 2–2 示例设备树

图中显示了典型的设备树中的叶和节点。

子树节点可以有一个或多个子节点。叶节点表示各个设备。

显示设备树

设备树可以采用以下三种方式显示:


注 –

/devices 仅显示将驱动程序配置到系统中的设备。prtconf(1M) 命令显示所有设备节点,而不管系统中是否存在设备驱动程序。


libdevinfo

libdevinfo 库提供用于访问所有公共设备配置数据的接口。有关接口列表,请参见 libdevinfo(3LIB) 手册页。

prtconf 命令

以下摘录的 prtconf(1M) 命令示例显示了系统中的所有设备。

System Configuration:  Sun Microsystems  sun4u
Memory size: 128 Megabytes
System Peripherals (Software Nodes):

SUNW,Ultra-5_10
    packages (driver not attached)
        terminal-emulator (driver not attached)
        deblocker (driver not attached)
        obp-tftp (driver not attached)
        disk-label (driver not attached)
        SUNW,builtin-drivers (driver not attached)
        sun-keyboard (driver not attached)
        ufs-file-system (driver not attached)
    chosen (driver not attached)
    openprom (driver not attached)
        client-services (driver not attached)
    options, instance #0
    aliases (driver not attached)
    memory (driver not attached)
    virtual-memory (driver not attached)
    pci, instance #0
        pci, instance #0
            ebus, instance #0
                auxio (driver not attached)
                power, instance #0
                SUNW,pll (driver not attached)
                se, instance #0
                su, instance #0
                su, instance #1
                ecpp (driver not attached)
                fdthree, instance #0
                eeprom (driver not attached)
                flashprom (driver not attached)
                SUNW,CS4231 (driver not attached)
            network, instance #0
            SUNW,m64B (driver not attached)
            ide, instance #0
                disk (driver not attached)
                cdrom (driver not attached)
                dad, instance #0
                sd, instance #15
        pci, instance #1
            pci, instance #0
                pci108e,1000 (driver not attached)
                SUNW,hme, instance #1
                SUNW,isptwo, instance #0
                    sd (driver not attached)
                    st (driver not attached)
                    sd, instance #0 (driver not attached)
                    sd, instance #1 (driver not attached)
                    sd, instance #2 (driver not attached)
                    ...
    SUNW,UltraSPARC-IIi (driver not attached)
    SUNW,ffb, instance #0
    pseudo, instance #0

/devices 目录

/devices 分层结构提供了表示设备树的名称空间。下面是 /devices 名称空间的缩写列表。样例输出对应于先前显示的示例设备树和 prtconf(1M) 输出。

/devices
/devices/pseudo
/devices/pci@1f,0:devctl
/devices/SUNW,ffb@1e,0:ffb0
/devices/pci@1f,0
/devices/pci@1f,0/pci@1,1
/devices/pci@1f,0/pci@1,1/SUNW,m64B@2:m640
/devices/pci@1f,0/pci@1,1/ide@3:devctl
/devices/pci@1f,0/pci@1,1/ide@3:scsi
/devices/pci@1f,0/pci@1,1/ebus@1
/devices/pci@1f,0/pci@1,1/ebus@1/power@14,724000:power_button
/devices/pci@1f,0/pci@1,1/ebus@1/se@14,400000:a
/devices/pci@1f,0/pci@1,1/ebus@1/se@14,400000:b
/devices/pci@1f,0/pci@1,1/ebus@1/se@14,400000:0,hdlc
/devices/pci@1f,0/pci@1,1/ebus@1/se@14,400000:1,hdlc
/devices/pci@1f,0/pci@1,1/ebus@1/se@14,400000:a,cu
/devices/pci@1f,0/pci@1,1/ebus@1/se@14,400000:b,cu
/devices/pci@1f,0/pci@1,1/ebus@1/ecpp@14,3043bc:ecpp0
/devices/pci@1f,0/pci@1,1/ebus@1/fdthree@14,3023f0:a
/devices/pci@1f,0/pci@1,1/ebus@1/fdthree@14,3023f0:a,raw
/devices/pci@1f,0/pci@1,1/ebus@1/SUNW,CS4231@14,200000:sound,audio
/devices/pci@1f,0/pci@1,1/ebus@1/SUNW,CS4231@14,200000:sound,audioctl
/devices/pci@1f,0/pci@1,1/ide@3
/devices/pci@1f,0/pci@1,1/ide@3/sd@2,0:a
/devices/pci@1f,0/pci@1,1/ide@3/sd@2,0:a,raw
/devices/pci@1f,0/pci@1,1/ide@3/dad@0,0:a
/devices/pci@1f,0/pci@1,1/ide@3/dad@0,0:a,raw
/devices/pci@1f,0/pci@1
/devices/pci@1f,0/pci@1/pci@2
/devices/pci@1f,0/pci@1/pci@2/SUNW,isptwo@4:devctl
/devices/pci@1f,0/pci@1/pci@2/SUNW,isptwo@4:scsi

将驱动程序绑定到设备

除了构造设备树之外,内核还可确定用于管理设备的驱动程序。

将驱动程序绑定到设备指的是系统选择用于管理特定设备的驱动程序的过程。绑定名称是将驱动程序与设备信息树连接在一起的唯一设备结点名称。对于设备树中的每个设备,系统都会尝试从已安装的驱动程序列表中选择一个驱动程序。

每个设备节点都有关联的 name 属性。可以在系统引导期间通过外部代理(如 PROM )或通过 driver.conf 配置文件指定此属性。无论在哪种情况下,name 属性都表示指定给设备树中的设备的节点名称节点名称是在 /devices 中可见并列在 prtconf(1M) 输出中的名称。

图 2–3 设备节点名称

图中显示了一个简单的设备节点名称示例。

设备节点也可以有关联的 compatible 属性。compatible 属性包含设备的一个或多个可能的驱动程序名称或驱动程序别名的有序列表。

系统使用 compatible 属性和 name 属性来为设备选择驱动程序。如果 compatible 属性存在,则系统会首先尝试将 compatible 属性的内容与系统中的驱动程序匹配。系统将从 compatible 属性列表的第一个驱动程序名称开始,尝试将该驱动程序名称与系统中的已知驱动程序匹配。系统将会处理该列表中的每一项,直到找到匹配项或者到达列表结尾。

如果 name 属性或 compatible 属性的内容与系统中的某个驱动程序匹配,则将该驱动程序绑定到设备节点。如果未找到匹配项,则不会将任何驱动程序绑定到设备节点。

通用设备名称

某些设备将通用设备名称指定为 name 属性的值。通用设备名称用于描述设备的功能,不实际标识设备的特定驱动程序。例如,SCSI 主机总线适配器可能具有通用设备名称 scsi。以太网设备可能具有通用设备名称 ethernet

通过 compatible 属性,系统可以确定具有通用设备名称的设备的备用驱动程序名称,例如,glm 对应于 scsi HBA 设备驱动程序,hme 对应于 ethernet 设备驱动程序。

具有通用设备名称的设备需要提供 compatible 属性。


注 –

有关通用设备名称的完整说明,请参见 IEEE 1275 Open Firmware Boot Standard。


下图显示了具有特定设备名称的设备节点。驱动程序绑定名称 SUNW,ffb 与设备节点名称同名。

图 2–4 特定驱动程序节点绑定

图中显示了使用特定设备名称的设备节点: SUNW, ffb。

下图显示了具有通用设备名称 display 的设备节点。驱动程序绑定名称 SUNW,ffbcompatible 属性驱动程序列表中与系统驱动程序列表中的驱动程序匹配的第一个名称。在这种情况下,display 是帧缓存器的通用设备名称。

图 2–5 通用驱动程序节点绑定

图中显示了使用通用设备名称的设备节点: display。