`
mryufeng
  • 浏览: 982383 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

抄书 Drivers in general

阅读更多
3.2 The driver
Although Erlang drivers in general may be beyond the scope of this document, a brief introduction seems to be in place.

3.2.1 Drivers in general
An Erlang driver is a native code module written in C (or assembler) which serves as an interface for some special operating system service. This is a general mechanism that is used throughout the Erlang emulator for all kinds of I/O. An Erlang driver can be dynamically linked (or loaded) to the Erlang emulator at runtime by using the erl_ddll Erlang module. Some of the drivers in OTP are however statically linked to the runtime system, but that's more an optimization than a necessity.

The driver data-types and the functions available to the driver writer are defined in the header file erl_driver.h (there is also an deprecated version called driver.h, don't use that one.) seated in Erlang's include directory (and in $ERL_TOP/erts/emulator/beam in the source code distribution). Refer to that file for function prototypes etc.

When writing a driver to make a communications protocol available to Erlang, one should know just about everything worth knowing about that particular protocol. All operation has to be non blocking and all possible situations should be accounted for in the driver. A non stable driver will affect and/or crash the whole Erlang runtime system, which is seldom what's wanted.

The emulator calls the driver in the following situations:

1. When the driver is loaded. This call-back has to have a special name and will inform the emulator of what call-backs should be used by returning a pointer to a ErlDrvEntry struct, which should be properly filled in (see below).

2. When a port to the driver is opened (by a open_port call from Erlang). This routine should set up internal data structures and return an opaque data entity of the type ErlDrvData, which is a data-type large enough to hold a pointer. The pointer returned by this function will be the first argument to all other call-backs concerning this particular port. It is usually called the port handle. The emulator only stores the handle and does never try to interpret it, why it can be virtually anything (well anything not larger than a pointer that is) and can point to anything if it is a pointer. Usually this pointer will refer to a structure holding information about the particular port, as i t does in our example.

3. When an Erlang process sends data to the port. The data will arrive as a buffer of bytes, the interpretation is not defined, but is up to the implementor. This call-back returns nothing to the caller, answers are sent to the caller as messages (using a routine called driver_output available to all drivers). There is also a way to talk in a synchronous way to drivers, described below. There can be an additional call-back function for handling data that is fragmented (sent in a deep io-list). That interface will get the data in a form suitable for Unix writev rather than in a single buffer. There is no need for a distribution driver to implement such a call-back, so we wont.

4. When a file descriptor is signaled for input. This call-back is called when the emulator detects input on a file descriptor which the driver has marked for monitoring by using the interface driver_select. The mechanism of driver select makes it possible to read non blocking from file descriptors by calling driver_select when reading is needed and then do the actual reading in this call-back (when reading is actually possible). The typical scenario is that driver_select is called when an Erlang process orders a read operation, and that this routine sends the answer when data is available on the file descriptor.

5. When a file descriptor is signaled for output. This call-back is called in a similar way as the previous, but when writing to a file descriptor is possible. The usual scenario is that Erlang orders writing on a file descriptor and that the driver calls driver_select. When the descriptor is ready for output, this call-back is called an the driver can try to send the output. There may of course be queuing involved in such operations, and there are some convenient queue routines available to the driver writer to use in such situations.

6. When a port is closed, either by an Erlang process or by the driver calling one of the driver_failure_XXX routines. This routine should clean up everything connected to one particular port. Note that when other call-backs call a driver_failure_XXX routine, this routine will be immediately called and the call-back routine issuing the error can make no more use of the data structures for the port, as this routine surely has freed all associated data and closed all file descriptors. If the queue utility available to driver writes is used, this routine will however not be called until the queue is empty.

7. When an Erlang process calls erlang:driver_control/2, which is a synchronous interface to drivers. The control interface is used to set driver options, change states of ports etc. We'll use this interface quite a lot in our example.

8. When a timer expires. The driver can set timers with the function driver_set_timer. When such timers expire, a specific call-back function is called. We will not use timers in our example.

9. When the whole driver is unloaded. Every resource allocated by the driver should be freed.


分享到:
评论

相关推荐

    Design_of_High_Voltage_xDSL_Line_Drivers_in_Standard_CMOS

    Design of High Voltage xDSL Line Drivers in Standard CMOS PASSCODE: 123456

    Linux Device Drivers Development: Develop customized drivers for embedded Linux

    Linux Device Drivers Development: Develop customized drivers for embedded Linux AZW3版(kindle格式),可以用开源软件calibre-ebook(https://calibre-ebook.com/)打开,里面的图片可以用该软件还原到原本的大...

    Linux Device Drivers.

    - **《Linux Device Drivers》一书**:这本书作为经典的参考指南,已经帮助无数程序员掌握了如何为Linux操作系统支持计算机外围设备以及如何为新硬件开发软件。 - **新版书籍更新**:第三版覆盖了Linux内核2.6的重要...

    CP2101_Drivers

    CP2101_Drivers CP2101_Drivers CP2101_Drivers

    Flashtool-drivers.zip

    《索尼手机刷机驱动详解——Flashtool-drivers.zip深度解析》 在智能手机的世界里,刷机是一项深受技术爱好者喜爱的操作,它可以让用户自定义系统、优化性能或尝试新的功能。尤其对于索尼手机用户来说,拥有合适的...

    Linux Device Drivers.pdf

    《Linux Device Drivers》这本书主要关注的是为Linux系统编写设备驱动程序。这不仅是针对新硬件产品不断涌现的需求,也包括理解Linux内核的工作原理以及如何根据自身需求或兴趣对其进行适应和修改。Linux作为一个...

    drivers_drivers_linux_Kernel_

    这个压缩包“drivers_drivers_linux_Kernel_”包含了书中的样例代码,是学习和理解Linux驱动开发的重要资源。 Linux驱动程序是连接硬件与操作系统之间的桥梁,它们负责向Linux内核提供与硬件交互的接口,使得内核...

    最新的Selenium Client Drivers

    Selenium Client Drivers是Selenium WebDriver的核心组成部分,它们是用于与不同的浏览器进行交互的接口。Selenium WebDriver是一个自动化测试工具,允许开发者模拟用户行为并进行网页应用程序的自动化测试。在这个...

    Linux Device Drivers

    Linux Device Drivers, 2nd Edition, In PDF Format Preface Chapter 1: An Introduction to Device Drivers Chapter 2: Building and Running Modules Chapter 3: Char Drivers Chapter 4: Debugging ...

    Essential Linux Device Drivers

    Drivers in User Space Process Scheduling and Response Times Accessing I/O Regions Accessing Memory Regions User Mode SCSI User Mode USB User Mode I2C UIO Looking at the Sources Chapter 20. ...

    Drivers of Finished-Goods Inventory in the U.S. Automobile Industry

    ### 美国汽车行业中成品库存的驱动因素 #### 引言 汽车制造业对全球经济具有重要影响,并在产品设计和制造技术方面产生了诸多创新(例如,装配线、准时制库存系统、看板管理等)。因此,它一直是大量实证研究的...

    P2K Drivers 2.9

    《P2K Drivers 2.9:E98设备刷机关键驱动详解》 在智能手机领域,刷机是一项常见的操作,它可以为用户带来更个性化的系统体验或解决设备问题。"P2K Drivers 2.9"是针对E98手机进行刷机的重要驱动程序,本文将详细...

    linux device drivers 3rd(chm+source code)

    《Linux Device Drivers 3rd》是Linux设备驱动程序开发领域的一本经典著作,结合源代码进行学习,对于想要深入理解Linux内核以及进行驱动开发的工程师来说,是必不可少的参考资料。这本书详细阐述了如何为Linux操作...

    Linux DMA in Device Drivers

    The goal of this session is to help users understand the Linux kernel DMA framework and how it can be used in a device driver

    Essential Linux Device Drivers + 源码

    《Essential Linux Device Drivers》是Linux世界中一本非常重要的书籍,它深入浅出地介绍了Linux设备驱动程序的开发和工作原理。这本书对于那些想要理解Linux内核如何与硬件交互,或者想要编写自己的设备驱动程序的...

    SQL Server Microsoft JDBC Drivers

    SQL Server Microsoft JDBC Drivers

Global site tag (gtag.js) - Google Analytics