驱动程序的角色

作为一个程序员, 你能够对你的驱动作出你自己的选择, 并且在所需的编程时间和结果的灵活性之间, 选择一个可接受的平衡. 尽管说一个驱动是"灵活"的, 听起来有些奇怪, 但是我们喜欢这个字眼, 因为它强调了一个驱动程序的角色是提供机制, 而不是策略.

机制和策略的区分是其中一个在 Unix 设计背后的最好观念. 大部分的编程问题其实可以划分为两部分:" 提供什么能力"(机制) "如何使用这些能力"(策略). 如果这两方面由程序的不同部分来表达, 或者甚至由不同的程序共同表达, 软件包是非常容易开发和适应特殊的需求.

例如, 图形显示的 Unix 管理划分为 X 服务器, 它理解硬件以及提供了统一的接口给用户程序, 还有窗口和会话管理器, 它实现了一个特别的策略, 而对硬件一无所知. 人们可以在不同的硬件上使用相同的窗口管理器, 而且不同的用户可以在同一台工作站上运行不同的配置. 甚至完全不同的桌面环境, 例如 KDE 和 GNOME, 可以在同一系统中共存. 另一个例子是 TCP/IP 网络的分层结构: 操作系统提供 socket 抽象层, 它对要传送的数据而言不实现策略, 而不同的服务器负责各种服务( 以及它们的相关策略). 而且, 一个服务器, 例如 ftpd 提供文件传输机制, 同时用户可以使用任何他们喜欢的客户端; 无论命令行还是图形客户端都存在, 并且任何人都能编写一个新的用户接口来传输文件.

在驱动相关的地方, 机制和策略之间的同样的区分都适用. 软驱驱动是不含策略的--它的角色仅仅是将磁盘表现为一个数据块的连续阵列. 系统的更高级部分提供了策略, 例如谁可以存取软驱驱动, 这个软驱是直接存取还是要通过一个文件系统, 以及用户是否可以加载文件系统到这个软驱. 因为不同的环境常常需要不同的使用硬件的方式, 尽可能对策略透明是非常重要的.

在编写驱动时, 程序员应当特别注意这个基础的概念: 编写内核代码来存取硬件, 但是不能强加特别的策略给用户, 因为不同的用户有不同的需求. 驱动应当做到使硬件可用, 将所有关于如何使用硬件的事情留给应用程序. 一个驱动, 这样, 就是灵活的, 如果它提供了对硬件能力的存取, 没有增加约束. 然而, 有时必须作出一些策略的决定. 例如, 一个数字 I/O 驱动也许只提供对硬件的字符存取, 以便避免额外的代码处理单个位.

你也可以从不同的角度看你的驱动: 它是一个存在于应用程序和实际设备间的软件层. 驱动的这种特权的角色允许驱动程序员y严密地选择设备应该如何表现: 不同的驱动可以提供不同的能力, 甚至是同一个设备. 实际的驱动设计应当是在许多不同考虑中的平衡. 例如, 一个单个设备可能由不同的程序并发使用, 驱动程序员有完全的自由来决定如何处理并发性. 你能在设备上实现内存映射而不依赖它的硬件能力, 或者你能提供一个用户库来帮助应用程序员在可用的原语之上实现新策略, 等等. 一个主要的考虑是在展现给用户尽可能多的选项, 和你不得不花费的编写驱动的时间之间做出平衡, 还有需要保持事情简单以避免错误潜入.

对策略透明的驱动有一些典型的特征. 这些包括支持同步和异步操作, 可以多次打开的能力, 利用硬件全部能力, 没有软件层来"简化事情"或者提供策略相关的操作. 这样的驱动不但对他们的最终用户好用, 而且证明也是易写易维护的. 成为策略透明的实际是一个共同的目标, 对软件设计者来说.

许多设备驱动, 确实, 是与用户程序一起发行的, 以便帮助配置和存取目标设备. 这些程序包括简单的工具到完全的图形应用. 例子包括 tunelp 程序, 它调整并口打印机驱动如何操作, 还有图形的 cardctl 工具, 它是 PCMCIA 驱动包的一部分. 经常会提供一个客户库, 它提供了不需要驱动自身实现的功能.

本书的范围是内核, 因此我们尽力不涉及策略问题, 应用程序, 以及支持库. 有时我们谈论不同的策略以及如何支持他们, 但是我们不会进入太多有关使用设备的程序的细节, 或者是他们强加的策略的细节. 但是, 你应当理解, 用户程序是一个软件包的构成部分, 并且就算是对策略透明的软件包在发行时也会带有配置文件, 来对底层的机制应用缺省的动作。

The Role of the Device Driver

As a programmer, you are able to make your own choices about your driver, and choose an acceptable trade-off between the programming time required and the flexibility of the result. Though it may appear strange to say that a driver is “flexible,” we like this word because it emphasizes that the role of a device driver is providing mechanism, notpolicy.

The distinction between mechanism and policy is one of the best ideas behind the Unix design. Most programming problems can indeed be split into two parts: “what capabilities are to be provided” (the mechanism) and “how those capabilities can be used” (the policy). If the two issues are addressed by different parts of the program, or even by different programs altogether, the software package is much easier to develop and to adapt to particular needs.

For example, Unix management of the graphic display is split between the X server, which knows the hardware and offers a unified interface to user programs, and the window and session managers, which implement a particular policy without knowing anything about the hardware. People can use the same window manager on different hardware, and different users can run different configurations on the same workstation. Even completely different desktop environments, such as KDE and GNOME, can coexist on the same system. Another example is the layered structure of TCP/IP networking: the operating system offers the socket abstraction, which implements no policy regarding the data to be transferred, while different servers are in charge of the services (and their associated policies). Moreover, a server like ftpd provides the file transfer mechanism, while users can use whatever client they prefer; both command-line and graphic clients exist, and anyone can write a new user interface to transfer files.

Where drivers are concerned, the same separation of mechanism and policy applies. The floppy driver is policy free—its role is only to show the diskette as a continuous array of data blocks. Higher levels of the system provide policies, such as who may access the floppy drive, whether the drive is accessed directly or via a filesystem, and whether users may mount filesystems on the drive. Since different environments usually need to use hardware in different ways, it’s important to be as policy free as possible.

When writing drivers, a programmer should pay particular attention to this fundamental concept: write kernel code to access the hardware, but don’t force particular policies on the user, since different users have different needs. The driver should deal with making the hardware available, leaving all the issues abouthowto use the hardware to the applications. A driver, then, is flexible if it offers access to the hardware capabilities without adding constraints. Sometimes, however, some policy decisions must be made. For example, a digital I/O driver may only offer byte-wide access to the hardware in order to avoid the extra code needed to handle individual bits.

You can also look at your driver from a different perspective: it is a software layer that lies between the applications and the actual device. This privileged role of the driver allows the driver programmer to choose exactly how the device should appear: different drivers can offer different capabilities, even for the same device. The actual driver design should be a balance between many different considerations. For instance, a single device may be used concurrently by different programs, and the driver programmer has complete freedom to determine how to handle concurrency.

You could implement memory mapping on the device independently of its hardware capabilities, or you could provide a user library to help application programmers implement new policies on top of the available primitives, and so forth. One major consideration is the trade-off between the desire to present the user with as many options as possible and the time you have to write the driver, as well as the need to keep things simple so that errors don’t creep in. Policy-free drivers have a number of typical characteristics. These include support for both synchronous and asynchronous operation, the ability to be opened multiple times, the ability to exploit the full capabilities of the hardware, and the lack of software layers to “simplify things” or provide policy-related operations. Drivers of this sort not only work better for their end users, but also turn out to be easier to write and maintain as well. Being policy-free is actually a common target for software designers.

Many device drivers, indeed, are released together with user programs to help with configuration and access to the target device. Those programs can range from simple utilities to complete graphical applications. Examples include thetunelp program, which adjusts how the parallel port printer driver operates, and the graphicalcardctl utility that is part of the PCMCIA driver package. Often a client library is provided as well, which provides capabilities that do not need to be implemented as part of the driver itself.

The scope of this book is the kernel, so we try not to deal with policy issues or with application programs or support libraries. Sometimes we talk about different policies and how to support them, but we won’t go into much detail about programs using the device or the policies they enforce. You should understand, however, that user programs are an integral part of a software package and that even policy-free packages are distributed with configuration files that apply a default behavior to the underlying mechanisms.

From: LDD3

转载于:https://www.cnblogs.com/embedded-linux/p/5941241.html

驱动思想之机制和策略相关推荐

  1. 驱动设计思想(机制、策略、分离、分层)

    1.机制和策略 (1)机制就是提供什么功能,策略就是怎么使用这些功能.在编写驱动时需要在编程时间和驱动的灵活性之间取一个可接受的折中,驱动提供机制,尽量不提供策略,策略让上层应用去做. (2)机制和策 ...

  2. 机制和策略相分离原则

    这些天大刀阔斧修改了我们的alsa音频驱动,更深切理解了机制和策略分离的重要性. 说来惭愧,Linux Device Drivers一书翻来覆去查阅了无数遍,却忽视了绪论中的一句话:区分机制和策略是U ...

  3. 机制与策略——陈莉君

    转自:http://www.zeuux.com/blog/content/1729/ Unix/Linux的接口设计有一句通用的格言"提供机制而不是策略".区别对待机制(mecha ...

  4. 计算机系统备份的原则和策略,计算机系统数据备份机制与策略

    计算机系统数据备份机制与策略 20年第5 05期 华中电力 第 l卷 8 计算机系统数据备份机制与策略 耿煜 (樊学院机械系,北襄樊襄湖 4 15 ) 4 03 摘要:针对当今计算环境中不断增长的数据 ...

  5. Linux看硬盘同步,从磁盘同步看linux的机制和策略

    linux操作系统是典型的机制和策略相分离的系统,机制和策略一直是设计领域一直关注的重要问题,它们的结合程度直接关系到系统的稳定性和可扩展性.今天做了一个linux上膝上电脑的电源管理的脚本,颇有感触 ...

  6. linux内核ppt刘小明,【陈老师华为北研所讲座PPT】从机制与策略探究Linux内核设计之道(4)...

    原标题:[陈老师华为北研所讲座PPT]从机制与策略探究Linux内核设计之道(4) 解放编译程序,以统一的方式分配逻辑地址. 首先内核通过映射机制把进程的虚拟地址映射到物理地址,在进程运行时,如果内核 ...

  7. Android Binder驱动的工作机制之要旨

    最近,看了不少Android内核分析的书籍.文章及Android源程序.感觉自己对Android Binder的工作机制算是有了个彻底的理解. 但是,自己是花了很多时间和精力之后才达到这一点的.对于大 ...

  8. JDBC驱动加载机制详解以及spi机制

    首先有两个问题: 1.java连接数据库时是否真的需要加载驱动? 2.JDBC如何区分多个驱动? 以下摘自:https://blog.csdn.net/buqutianya/article/detai ...

  9. 【天光学术】行政管理论文:事业单位行政管理机制创新策略探究(节选)

    [摘要]基于当前事业单位体制改革不断深入的新形势下,事业单位行政管理工作的重要性得以突显.为了确保事业单位各项工作有序开展,需要重视行政管理机制的创新,全面提高事业单位行政管理工作的效率和质量. [关 ...

最新文章

  1. 什么是AWS Lambda?
  2. WebKit 框架解析
  3. ASP.NET2.0的multiview和wizard控件
  4. ubuntu自动挂载NTFS的方法
  5. 多态性与虚拟函数一个典型的例子第一步
  6. 03SpringMVC,Spring,Hibernate整合(Date时间转换)
  7. 《Neo4j全栈开发》_陈韶健
  8. golang mysql大量写入_Golang 实现分片读取http超大文件流和并发控制
  9. [js高手之路]从零开始打造一个javascript开源框架gdom与插件开发免费视频教程连载中...
  10. 【渝粤题库】国家开放大学2021春3935理工英语2题目
  11. JavaScript 函数(作用域以及闭包)
  12. TQ210——S5PV210串口通信
  13. Leetcode142. Linked List Cycle II环形链表2
  14. cpu在计算机系统中的地位和作用是什么,电脑的核心-中央处理器(CPU)及其在游戏中的作用...
  15. IOS逆向学习之路之iPod touch4 越狱心得
  16. ASP网站访问量计数器
  17. oracle omf管理,使用OMF管理表空间
  18. Clickhouse其它类型表引擎(Live View、Null、URL)
  19. Redis【10】-Redis发布订阅
  20. win10安装Mysql8的两种方式(安装/卸载-图文教程)

热门文章

  1. Maven开发笔记(四)—— Maven中plugins和pluginManagement
  2. 带格式化参数的strcat宏定义
  3. 删除列表中满足一定条件的元素
  4. [Redux/Mobx] Redux由哪些组件构成?
  5. [react] 你有用过哪些React的UI库?它们的优缺点分别是什么
  6. 前端学习(3026):vue+element今日头条管理-让导航栏和路由对应起来
  7. [css] 怎么才能让图文不可复制?
  8. [css] 说出至少十条你理解的css规范
  9. [vue-cli]vue-cli提供了的哪几种脚手架模板?
  10. 前端学习(2829):block标签的使用