文件名称:编写设备驱动程序
文件大小:4.89MB
文件格式:PDF
更新时间:2016-04-22 07:24:33
编写 设备驱动程序
编写设备驱动程序》提供有关为面向字符的设备、面向块的设备、网络设备、SCSI目标和HBA设备以及USB设备开发SolarisolarisOperatingSystem,SolarisOS)驱动程序的信息。本书讨论了如何为符合SolarisOSDDI/DKI(DeviceDriver Interface/Driver-KernelInterface,设备驱动程序接口/驱动程序内核接口)的所有体系结构开发多线程可重入设备驱动程序。介绍了一种常用的驱动程序编写方法,该方法允许在编写驱动程序时忽略特定于平台的问题,如字节存储顺序(endianness)和数据排序等。 其他主题包括:强化Solaris驱动程序;电源管理;驱动程序自动配置;程控I/O;直接内存访问(DirectMemoryAccess,DMA);设备上下文管理;编译、安装和测试驱动程序;调试驱动程序以及将Solaris驱动程序移植到64位环境。 目录 前言........................................................................................................................................................29 第1部分 针对Solaris平台设计设备驱动程序..............................................................................................35 1 Solaris设备驱动程序概述 ................................................................................................................37 设备驱动程序基础知识.................................................................................................................... 37 什么是设备驱动程序? ............................................................................................................ 37 什么是设备驱动程序入口点? ............................................................................................... 38 设备驱动程序入口点 ........................................................................................................................ 38 通用于所有驱动程序的入口点 ............................................................................................... 39 用于块设备驱动程序的入口点 ............................................................................................... 41 用于字符设备驱动程序的入口点........................................................................................... 42 用于STREAMS设备驱动程序的入口点 ............................................................................... 43 用于内存映射设备的入口点 ................................................................................................... 44 网络设备驱动程序入口点 ........................................................................................................ 45 用于SCSIHBA驱动程序的入口点 ........................................................................................ 45 用于PC卡驱动程序的入口点 ................................................................................................. 46 设备驱动程序设计注意事项 ........................................................................................................... 46 DDI/DKI功能 ............................................................................................................................. 47 驱动程序上下文 ......................................................................................................................... 48 返回错误 ...................................................................................................................................... 49 动态内存分配 .............................................................................................................................. 49 热插拔 ........................................................................................................................................... 50 2 Solaris内核和设备树 .........................................................................................................................51 什么是内核? ..................................................................................................................................... 51 多线程执行环境 ......................................................................................................................... 53 3 虚拟内存 ...................................................................................................................................... 53 作为特殊文件的设备................................................................................................................. 53 DDI/DKI接口 ............................................................................................................................. 53 设备树概述.......................................................................................................................................... 54 设备树组件 .................................................................................................................................. 54 显示设备树 .................................................................................................................................. 56 将驱动程序绑定到设备 ............................................................................................................ 58 3 多线程 ...................................................................................................................................................61 锁定原语 .............................................................................................................................................. 61 驱动程序数据的存储类 ............................................................................................................ 61 互斥锁 ........................................................................................................................................... 62 读取器/写入器锁 ........................................................................................................................ 63 信号 ............................................................................................................................................... 63 线程同步 .............................................................................................................................................. 64 线程同步中的条件变量 ............................................................................................................ 64 cv_wait()和cv_timedwait()函数 ........................................................................................ 65 cv_wait_sig()函数 ................................................................................................................... 66 cv_timedwait_sig()函数 ........................................................................................................ 67 选择锁定方案 ..................................................................................................................................... 67 潜在的锁定缺点 ......................................................................................................................... 68 线程无法接收信号 ..................................................................................................................... 68 4 属性........................................................................................................................................................71 设备属性 .............................................................................................................................................. 71 设备属性名称 .............................................................................................................................. 72 创建和更新属性 ......................................................................................................................... 72 查找属性 ...................................................................................................................................... 72 prop_op()入口点 ....................................................................................................................... 74 5 管理事件和排队任务 .........................................................................................................................77 管理事件 .............................................................................................................................................. 77 事件介绍 ...................................................................................................................................... 77 使用ddi_log_sysevent()记录事件 ...................................................................................... 78 目录 编写设备驱动程序 • 2010年9月 4 定义事件特性 .............................................................................................................................. 80 排队任务 .............................................................................................................................................. 83 任务队列简介 .............................................................................................................................. 83 任务队列接口 .............................................................................................................................. 83 使用任务队列 .............................................................................................................................. 84 观察任务队列 .............................................................................................................................. 84 6 驱动程序自动配置 .............................................................................................................................87 驱动程序的装入和卸载.................................................................................................................... 87 驱动程序必需的数据结构 ............................................................................................................... 88 modlinkage结构 ......................................................................................................................... 88 modldrv结构 ................................................................................................................................ 89 dev_ops结构 ................................................................................................................................ 89 cb_ops结构 .................................................................................................................................. 90 可装入驱动程序接口 ........................................................................................................................ 91 _init()示例 ................................................................................................................................ 92 _fini()示例 ................................................................................................................................ 93 _info()示例 ................................................................................................................................ 93 设备配置概念 ..................................................................................................................................... 94 设备实例和实例编号................................................................................................................. 94 次要节点和次要设备号 ............................................................................................................ 95 probe()入口点 ........................................................................................................................... 95 attach()入口点 ......................................................................................................................... 97 detach()入口点 .......................................................................................................................102 getinfo()入口点 .....................................................................................................................103 使用设备ID ......................................................................................................................................105 注册设备ID ...............................................................................................................................105 注销设备ID ...............................................................................................................................106 7 设备访问:程控I/O..........................................................................................................................107 设备内存 ............................................................................................................................................107 管理设备和主机字节序之间的差别 ....................................................................................108 管理数据排序要求 ...................................................................................................................108 ddi_device_acc_attr结构 ....................................................................................................108 映射设备内存 ............................................................................................................................109 目录 5 映射设置示例 ............................................................................................................................109 设备访问函数 ...................................................................................................................................110 备用设备访问接口 ...................................................................................................................111 8 中断处理程序 ................................................................................................................................... 113 中断处理程序概述 ..........................................................................................................................113 设备中断 ............................................................................................................................................114 高级别中断 ................................................................................................................................114 传统中断 ....................................................................................................................................115 标准消息告知中断和扩展消息告知中断 ...........................................................................115 软件中断 ....................................................................................................................................116 DDI中断函数 ...................................................................................................................................117 中断功能函数 ............................................................................................................................117 中断初始化和销毁函数 ..........................................................................................................117 优先级管理函数 .......................................................................................................................118 软中断函数 ................................................................................................................................118 中断函数示例 ............................................................................................................................118 注册中断 ............................................................................................................................................119 注册传统中断 ............................................................................................................................119 注册MSI中断 ...........................................................................................................................122 中断资源管理 ...................................................................................................................................125 中断资源管理功能 ...................................................................................................................125 回调接口 ....................................................................................................................................126 中断请求接口 ............................................................................................................................128 中断资源管理实现样例 ..........................................................................................................130 中断处理程序功能 ..........................................................................................................................135 处理高级别中断 ...............................................................................................................................137 高级互斥锁 ................................................................................................................................137 高级别中断处理示例...............................................................................................................137 9 直接内存访问(DirectMemoryAccess,DMA) ..............................................................................141 DMA模型 ..........................................................................................................................................141 设备DMA的类型 ............................................................................................................................142 总线主控器DMA .....................................................................................................................142 第三方DMA ..............................................................................................................................142 目录 编写设备驱动程序 • 2010年9月 6 第一方DMA ..............................................................................................................................143 主机平台DMA的类型 ...................................................................................................................143 DMA软件组件:句柄、窗口和Cookie .....................................................................................143 DMA操作 ..........................................................................................................................................144 执行总线主控器DMA传送 ...................................................................................................144 执行第一方DMA传送 ............................................................................................................144 执行第三方DMA传送 ............................................................................................................145 DMA特性 ..................................................................................................................................145 管理DMA资源 ................................................................................................................................148 对象锁定 ....................................................................................................................................148 分配DMA句柄 .........................................................................................................................148 分配DMA资源 .........................................................................................................................149 确定最大突发流量大小 ..........................................................................................................151 分配专用DMA缓冲区 ............................................................................................................152 处理资源分配故障 ...................................................................................................................153 对DMA引擎进行编程 ............................................................................................................154 释放DMA资源 .........................................................................................................................155 释放DMA句柄 .........................................................................................................................156 取消DMA回调 .........................................................................................................................156 同步内存对象 ............................................................................................................................157 DMA窗口 ..........................................................................................................................................159 10 映射设备和内核内存 ...................................................................................................................... 163 内存映射概述 ...................................................................................................................................163 导出映射 ............................................................................................................................................163 segmap(9E)入口点 ....................................................................................................................163 devmap(9E)入口点 ....................................................................................................................165 将设备内存与用户映射相关联 ....................................................................................................166 将内核内存与用户映射相关联 ....................................................................................................168 为用户访问分配内核内存 ......................................................................................................168 将内核内存导出到应用程序 .................................................................................................170 释放为用户访问导出的内核内存.........................................................................................171 11 设备上下文管理 ............................................................................................................................... 173 设备上下文简介 ...............................................................................................................................173 目录 7 什么是设备上下文?...............................................................................................................173 上下文管理模型 .......................................................................................................................173 上下文管理操作 ...............................................................................................................................175 devmap_callback_ctl结构 ....................................................................................................175 用于设备上下文管理的入口点 .............................................................................................176 将用户映射与驱动程序通知关联.........................................................................................183 管理映射访问 ............................................................................................................................184 12 电源管理 ............................................................................................................................................ 187 电源管理框架 ...................................................................................................................................187 设备电源管理 ............................................................................................................................187 系统电源管理 ............................................................................................................................188 设备电源管理模型 ..........................................................................................................................188 电源管理组件 ............................................................................................................................189 电源管理状态 ............................................................................................................................189 电源级别 ....................................................................................................................................189 电源管理相关性 .......................................................................................................................191 设备的自动电源管理...............................................................................................................191 设备电源管理接口 ...................................................................................................................192 power()入口点 .........................................................................................................................193 系统电源管理模型 ..........................................................................................................................195 自动关闭阈值 ............................................................................................................................196 繁忙状态 ....................................................................................................................................196 硬件状态 ....................................................................................................................................196 系统的自动电源管理...............................................................................................................196 系统电源管理使用的入口点 .................................................................................................196 电源管理设备访问示例..................................................................................................................200 电源管理控制流程 ..........................................................................................................................201 电源管理接口的更改 ......................................................................................................................202 13 强化Solaris驱动程序 ......................................................................................................................205 Sun故障管理体系结构I/O故障服务 .........................................................................................205 什么是预测性自我修复? ......................................................................................................206 SolarisFaultManager ................................................................................................................206 错误处理 ....................................................................................................................................209 目录 编写设备驱动程序 • 2010年9月 8 诊断故障 ....................................................................................................................................222 事件注册表 ................................................................................................................................223 词汇表 .........................................................................................................................................223 资源 .............................................................................................................................................224 用于Solaris设备驱动程序的防御性编程方法 ..........................................................................224 使用单独的设备驱动程序实例 .............................................................................................225 独占使用DDI访问句柄 .........................................................................................................225 检测已损坏的数据 ...................................................................................................................225 DMA隔离 ..................................................................................................................................226 处理有问题的中断 ...................................................................................................................227 其他编程注意事项 ...................................................................................................................228 驱动程序强化测试工具..................................................................................................................229 故障注入 ....................................................................................................................................229 设置测试工具 ............................................................................................................................230 测试驱动程序 ............................................................................................................................231 使用脚本自动完成测试过程 .................................................................................................233 14 分层驱动程序接口(LayeredDriverInterface,LDI) ....................................................................237 LDI概述 .............................................................................................................................................237 内核接口 ............................................................................................................................................238 分层标识符-内核设备使用方 .............................................................................................238 分层驱动程序句柄-目标设备 .............................................................................................239 LDI内核接口示例 ....................................................................................................................242 用户接口 ............................................................................................................................................253 设备信息库接口 .......................................................................................................................253 列显系统配置命令接口 ..........................................................................................................254 设备用户命令接口 ...................................................................................................................257 第2部分 设计特定种类的设备驱动程序 .....................................................................................................259 15 字符设备驱动程序 .......................................................................................................................... 261 字符驱动程序结构概述..................................................................................................................261 字符设备自动配置 ..........................................................................................................................263 设备访问(字符驱动程序) .........................................................................................................264 目录 9 open()入口点(字符驱动程序) ........................................................................................264 close()入口点(字符驱动程序) ......................................................................................265 I/O请求处理 .....................................................................................................................................266 用户地址 ....................................................................................................................................266 向量化的I/O .............................................................................................................................266 同步I/O与异步I/O之间的差别 ..........................................................................................268 数据传输方法 ............................................................................................................................268 映射设备内存 ...................................................................................................................................274 对文件描述符执行多路复用I/O操作 ........................................................................................275 其他I/O控制 ....................................................................................................................................277 ioctl()入口点(字符驱动程序) ......................................................................................277 对有64位处理能力的设备驱动程序的I/O控制支持 .....................................................279 处理copyout()溢出 ................................................................................................................281 32位和64位数据结构宏................................................................................................................282 结构宏如何工作? ...................................................................................................................282 何时使用结构宏 .......................................................................................................................283 声明并初始化结构句柄 ..........................................................................................................283 结构句柄的操作 .......................................................................................................................283 其他操作 ....................................................................................................................................284 16 块设备驱动程序 ............................................................................................................................... 285 块驱动程序结构概述 ......................................................................................................................285 文件I/O ..............................................................................................................................................286 块设备自动配置 ...............................................................................................................................287 控制设备访问 ...................................................................................................................................288 open()入口点(块驱动程序) .............................................................................................288 close()入口点(块驱动程序)...........................................................................................290 strategy()入口点 ...................................................................................................................290 buf结构 ......................................................................................................................................291 同步数据传输(块驱动程序) ....................................................................................................292 异步数据传输(块驱动程序) ....................................................................................................296 检查是否有无效的buf请求 ..................................................................................................296 对请求进行排队 .......................................................................................................................296 开始第一个传输 .......................................................................................................................297 处理中断的设备 .......................................................................................................................298 目录 编写设备驱动程序 • 2010年9月 10 dump()和print()入口点 ..............................................................................................................299 dump()入口点(块驱动程序) .............................................................................................300 print()入口点(块驱动程序)...........................................................................................300 磁盘设备驱动程序 ..........................................................................................................................300 磁盘ioctl ..................................................................................................................................301 磁盘性能 ....................................................................................................................................301 17 SCSI目标驱动程序 ...........................................................................................................................303 目标驱动程序介绍 ..........................................................................................................................303 Sun公用SCSI体系结构概述 .........................................................................................................304 常规控制流程 ............................................................................................................................305 SCSA函数 ..................................................................................................................................305 硬件配置文件 ...................................................................................................................................306 声明和数据结构 ...............................................................................................................................307 scsi_device结构 .....................................................................................................................307 scsi_pkt结构(目标驱动程序) ........................................................................................308 SCSI目标驱动程序的自动配置 ....................................................................................................309 probe()入口点(SCSI目标驱动程序) .............................................................................310 attach()入口点(SCSI目标驱动程序) ...........................................................................311 detach()入口点(SCSI目标驱动程序) ...........................................................................314 getinfo()入口点(SCSI目标驱动程序).........................................................................314 资源分配 ............................................................................................................................................315 scsi_init_pkt()函数 .............................................................................................................315 scsi_sync_pkt()函数 .............................................................................................................316 scsi_destroy_pkt()函数 ......................................................................................................316 scsi_alloc_consistent_buf()函数 ...................................................................................316 scsi_free_consistent_buf()函数......................................................................................317 生成和传输命令 ...............................................................................................................................317 生成命令 ....................................................................................................................................317 设置目标功能 ............................................................................................................................318 传输命令 ....................................................................................................................................318 命令完成 ....................................................................................................................................319 重新使用包 ................................................................................................................................320 自动请求检测模式 ...................................................................................................................321 转储处理 ....................................................................................................................................322 目录 11 SCSI选项 ...........................................................................................................................................324 18 SCSI主机总线适配器驱动程序 .....................................................................................................327 主机总线适配器驱动程序介绍 ....................................................................................................327 SCSI接口 ...........................................................................................................................................328 SCSAHBA接口 ................................................................................................................................329 SCSAHBA入口点摘要 ...........................................................................................................329 SCSAHBA数据结构 ................................................................................................................330 按目标实例的数据 ...................................................................................................................335 传输结构克隆 ............................................................................................................................336 SCSAHBA函数 .........................................................................................................................337 HBA驱动程序的相关性和配置问题 ...........................................................................................338 声明和结构 ................................................................................................................................338 模块初始化入口点 ...................................................................................................................339 自动配置入口点 .......................................................................................................................341 SCSAHBA驱动程序入口点 ..........................................................................................................344 目标驱动程序实例初始化 ......................................................................................................345 资源分配 ....................................................................................................................................347 命令传输 ....................................................................................................................................356 功能管理 ....................................................................................................................................362 中止和重置管理 .......................................................................................................................366 动态重新配置 ............................................................................................................................368 SCSIHBA驱动程序特定问题 .......................................................................................................369 安装HBA驱动程序 .................................................................................................................369 HBA配置属性 ...........................................................................................................................370 x86目标驱动程序配置属性 ...................................................................................................371 排队支持 ............................................................................................................................................372 19 网络设备驱动程序 .......................................................................................................................... 373 GLDv3网络设备驱动程序框架 ....................................................................................................373 GLDv3MAC注册 .....................................................................................................................373 GLDv3功能 ...............................................................................................................................377 GLDv3数据路径 .......................................................................................................................380 GLDv3状态更改通知 ..............................................................................................................382 GLDv3网络统计信息 ..............................................................................................................382 目录 编写设备驱动程序 • 2010年9月 12 GLDv3属性 ...............................................................................................................................383 GLDv3接口汇总 .......................................................................................................................384 GLDv2网络设备驱动程序框架 ....................................................................................................386 GLDv2设备支持 .......................................................................................................................387 GLDv2DLPI提供者 .................................................................................................................388 GLDv2DLPI原语 .....................................................................................................................389 GLDv2I/O控制函数 ................................................................................................................390 GLDv2驱动程序需求 ..............................................................................................................391 GLDv2网络统计信息 ..............................................................................................................392 GLDv2声明和数据结构..........................................................................................................395 GLDv2函数参数 .......................................................................................................................399 GLDv2入口点 ...........................................................................................................................400 GLDv2返回值 ...........................................................................................................................403 GLDv2服务例程 .......................................................................................................................403 20 USB驱动程序.....................................................................................................................................407 Solaris环境中的USB .......................................................................................................................407 USBA2.0框架 ............................................................................................................................407 USB客户机驱动程序 ...............................................................................................................408 绑定客户机驱动程序 ......................................................................................................................410 USB设备如何显示在系统中 .................................................................................................410 USB设备和Solaris设备树 ......................................................................................................410 兼容设备名称 ............................................................................................................................410 具有多个接口的设备...............................................................................................................412 检查设备驱动程序绑定 ..........................................................................................................413 基本设备访问 ...................................................................................................................................413 连接客户机驱动程序之前 ......................................................................................................413 描述符树 ....................................................................................................................................413 注册驱动程序以获取设备访问权限 ....................................................................................415 设备通信 ............................................................................................................................................416 USB端点.....................................................................................................................................416 缺省管道 ....................................................................................................................................417 管道状态 ....................................................................................................................................417 打开管道 ....................................................................................................................................417 关闭管道 ....................................................................................................................................418 目录 13 数据传输 ....................................................................................................................................418 刷新管道 ....................................................................................................................................424 设备状态管理 ...................................................................................................................................424 热插拔USB设备.......................................................................................................................425 电源管理 ....................................................................................................................................427 序列化 .........................................................................................................................................431 实用程序函数 ...................................................................................................................................431 设备配置工具 ............................................................................................................................431 其他实用程序函数 ...................................................................................................................433 USB设备驱动程序样例 ..................................................................................................................434 第3部分 生成设备驱动程序 ...........................................................................................................................435 21 编译、装入、打包和测试驱动程序............................................................................................ 437 驱动程序开发摘要 ..........................................................................................................................437 驱动程序代码布局 ..........................................................................................................................438 头文件 .........................................................................................................................................438 源文件 .........................................................................................................................................439 配置文件 ....................................................................................................................................439 准备安装驱动程序 ..........................................................................................................................440 编译和链接驱动程序...............................................................................................................440 模块相关性 ................................................................................................................................441 编写硬件配置文件 ...................................................................................................................442 安装、更新和删除驱动程序 .........................................................................................................442 将驱动程序复制到模块目录 .................................................................................................442 使用add_drv安装驱动程序...................................................................................................443 更新驱动程序信息 ...................................................................................................................444 删除驱动程序 ............................................................................................................................444 装入和卸载驱动程序 ......................................................................................................................444 驱动程序打包 ...................................................................................................................................445 软件包后安装 ............................................................................................................................445 软件包预删除 ............................................................................................................................446 驱动程序测试条件 ..........................................................................................................................447 配置测试 ....................................................................................................................................447 功能测试 ....................................................................................................................................447 目录 编写设备驱动程序 • 2010年9月 14 错误处理 ....................................................................................................................................447 测试装入和卸载 .......................................................................................................................448 压力、性能和互操作性测试 .................................................................................................448 DDI/DKI兼容性测试 ..............................................................................................................449 安装和打包测试 .......................................................................................................................449 测试特定类型驱动程序 ..........................................................................................................449 22 调试、测试和调优设备驱动程序 ................................................................................................ 451 测试驱动程序 ...................................................................................................................................451 启用Deadman功能以避免硬挂起 .......................................................................................451 使用串行连接进行测试 ..........................................................................................................452 设置测试模块 ............................................................................................................................454 避免测试系统中发生数据丢失 .............................................................................................456 恢复设备目录 ............................................................................................................................458 调试工具 ............................................................................................................................................459 事后调试 ....................................................................................................................................460 使用kmdb内核调试程序 .........................................................................................................460 使用mdb模块调试程序 ...........................................................................................................463 使用kmdb和mdb执行的有用调试任务 ...............................................................................464 调优驱动程序 ...................................................................................................................................471 内核统计信息 ............................................................................................................................471 用于动态检测过程的DTrace.................................................................................................476 23 推荐的编码方法 ............................................................................................................................... 477 调试准备方法 ...................................................................................................................................477 使用唯一前缀来避免内核符号冲突 ....................................................................................477 使用cmn_err()记录驱动程序活动......................................................................................478 使用ASSERT()捕捉无效假设 ................................................................................................478 使用mutex_owned()验证和记录锁定要求 .........................................................................478 使用条件编译在开销较大的调试功能之间切换 ..............................................................479 将变量声明为可变变量..................................................................................................................480 可维护性 ............................................................................................................................................481 定期运行状况检查 ...................................................................................................................481