⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 char device driver interfaces.htm

📁 What is this ``device driver stuff anyway? Here s a very short introduction to the concept.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<html><head><title>Implementing Character Device Driver Interfaces</title>

<meta name="ROBOTS" content="NOFOLLOW">
<meta name="DESCRIPTION" content="Writing Device Drivers Tutorial: DIGITAL UNIX Version 4.0B documentation set">
<meta name="AUTHOR" content="Copyright (c) Digital Equipment Corporation 1996. All Rights Reserved."></head>
<body>
<a name="ImplCharDrvRoutines"></a>
<p></p><hr><p align="center">
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/Digital_UNIX_Bookshelf.html"><img src="char%20device%20driver%20interfaces_files/BOOKSHELF.GIF" alt="[Return to Library]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/TOC.html"><img src="char%20device%20driver%20interfaces_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut11.html"><img src="char%20device%20driver%20interfaces_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<img src="char%20device%20driver%20interfaces_files/BLANK.GIF" border="0">
<a href="#ImplementTheReadRoutine"><img src="char%20device%20driver%20interfaces_files/NEXT.GIF" alt="[Next Section]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut13.html"><img src="char%20device%20driver%20interfaces_files/FF.GIF" alt="[Next Chapter]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/INDEX.html"><img src="char%20device%20driver%20interfaces_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="char%20device%20driver%20interfaces_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h1>8&nbsp;&nbsp;&nbsp;&nbsp;Implementing Character Device Driver Interfaces</h1>
<p>
Character device drivers can contain the following sections:
</p><ul>
<p></p><li>
A read and write device section, which
contains a
<tt>read</tt>
interface and a
<tt>write</tt>
interface
<p></p></li><li>
A reset section, which
contains a
<tt>reset</tt>
interface
<p></p></li><li>
A select section, which
contains a
<tt>select</tt>
interface
<p></p></li><li>
A stop section, which
contains a
<tt>stop</tt>
interface
<p></p></li><li>
A memory map section, which
contains an
<tt>mmap</tt>
(memory map) interface.
</li></ul><p>
The following sections explain how to implement or set up each of these
interfaces.
<a name="ImplementTheReadRoutine"></a>
</p><p></p><hr><p align="center">
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/Digital_UNIX_Bookshelf.html"><img src="char%20device%20driver%20interfaces_files/BOOKSHELF.GIF" alt="[Return to Library]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/TOC.html"><img src="char%20device%20driver%20interfaces_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#ImplCharDrvRoutines"><img src="char%20device%20driver%20interfaces_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<img src="char%20device%20driver%20interfaces_files/BLANK.GIF" border="0">
<a href="#Thedev_tDataType"><img src="char%20device%20driver%20interfaces_files/NEXT.GIF" alt="[Next Section]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut13.html"><img src="char%20device%20driver%20interfaces_files/FF.GIF" alt="[Next Chapter]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/INDEX.html"><img src="char%20device%20driver%20interfaces_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="char%20device%20driver%20interfaces_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h2>
8.1&nbsp;&nbsp;&nbsp;&nbsp;Implementing the read Interface
</h2>
<p>
<a name="nx_id_417"></a>
A device driver's
<tt>read</tt>
interface performs the tasks necessary to read data from a device.
The kernel calls the driver's
<tt>read</tt>
interface for character device drivers
on behalf of applications that make a
<tt>read</tt>
system call.
The code associated with a
<tt>read</tt>
interface resides in the read and write device section of the device
driver.
<a name="nx_id_418"></a>
You specify the entry point for the driver's
<tt>read</tt>
interface
in a
<tt>dsent</tt>
structure.
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut10.html#dsentDataStruct">Section 6.6.5.1</a>
describes the
<tt>dsent</tt>
structure.
<cite>Writing Device Drivers: Reference</cite>
provides a reference page that gives additional information on the
arguments and tasks associated with
a
<tt>read</tt>
interface.
</p><p>
To implement a
<tt>read</tt>
interface you must understand the
<tt>dev_t</tt>
data type and the
<tt>uio</tt>
data structure.
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut12.html#Thedev_tDataType">Section 8.1.1</a>
and
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut12.html#theuio_struct">Section 8.1.2</a>
discuss these topics.
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut12.html#Thedev_tDataType">Section 8.1.1</a>
discusses how to set up a
<tt>read</tt>
interface.
<a name="Thedev_tDataType"></a>
</p><p></p><hr><p align="center">
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/Digital_UNIX_Bookshelf.html"><img src="char%20device%20driver%20interfaces_files/BOOKSHELF.GIF" alt="[Return to Library]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/TOC.html"><img src="char%20device%20driver%20interfaces_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#ImplCharDrvRoutines"><img src="char%20device%20driver%20interfaces_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#ImplementTheReadRoutine"><img src="char%20device%20driver%20interfaces_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#GetDevMajorNumb"><img src="char%20device%20driver%20interfaces_files/NEXT.GIF" alt="[Next Section]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut13.html"><img src="char%20device%20driver%20interfaces_files/FF.GIF" alt="[Next Chapter]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/INDEX.html"><img src="char%20device%20driver%20interfaces_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="char%20device%20driver%20interfaces_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
8.1.1&nbsp;&nbsp;&nbsp;&nbsp;Using the dev_t Data Type
</h3>
<p>
<a name="nx_id_419"></a>
<a name="nx_id_420"></a>
<a name="nx_id_421"></a>
The
<tt>dev_t</tt>
data type is a type definition of the major and minor device numbers.
Device driver writers use the
<tt>dev_t</tt>
data type to represent a device's major and minor numbers.
This data type is an abstraction of the internal representations
of the major and minor numbers. 
Driver writers do not need to know how the system internally
represents the major and minor numbers.
To ensure maximum portability of the device driver, use the
<tt>major</tt>
interface to extract the major number portion of this internal
representation and use the
<tt>minor</tt>
interface to extract the minor number portion of this internal
representation.
</p><p>
The following character driver interfaces take a
<tt>dev_t</tt>
as the first argument:
<tt>read</tt>,
<tt>write</tt>,
<tt>select</tt>,
and
<tt>mmap</tt>
(memory map).
The following sections describe how to use the
<tt>major</tt>
and
<tt>minor</tt>
interfaces.
<a name="GetDevMajorNumb"></a>
</p><p></p><hr><p align="center">
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/Digital_UNIX_Bookshelf.html"><img src="char%20device%20driver%20interfaces_files/BOOKSHELF.GIF" alt="[Return to Library]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/TOC.html"><img src="char%20device%20driver%20interfaces_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#ImplCharDrvRoutines"><img src="char%20device%20driver%20interfaces_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#Thedev_tDataType"><img src="char%20device%20driver%20interfaces_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#GetDevMinorNumb"><img src="char%20device%20driver%20interfaces_files/NEXT.GIF" alt="[Next Section]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut13.html"><img src="char%20device%20driver%20interfaces_files/FF.GIF" alt="[Next Chapter]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/INDEX.html"><img src="char%20device%20driver%20interfaces_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="char%20device%20driver%20interfaces_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
8.1.1.1&nbsp;&nbsp;&nbsp;&nbsp;Getting the Device Major Number with major
</h3>
<p>
To get the device major number, call the
<tt>major</tt>
interface.
The following code fragment shows a call to this interface:
</p><p>
</p><pre><br>.<br>.<br>.<br>
xxread(dev, uio, flag)
dev_t dev; <a name="co_id_63_rtn_1"></a><a href="#co_id_63_1"><strong>[1]</strong></a>
struct uio *uio;
int flag;
<br>.<br>.<br>.<br>
int major_number = major(dev); <a name="co_id_63_rtn_2"></a><a href="#co_id_63_2"><strong>[2]</strong></a>
<br>.<br>.<br>.<br>
</pre>
<p>
</p><ol>
<p></p><li>
<a name="co_id_63_1"></a>
<a name="nx_id_422"></a>
Declares an argument that specifies the major and minor numbers for a
specific
<tt>XX</tt>
device.
The major and minor number is passed to the
<tt>xxread</tt>
interface by the kernel as a result of an application's
<tt>read</tt>
system call.
<a href="#co_id_63_rtn_1">[Return to example]</a>
<p></p></li><li>
<a name="co_id_63_2"></a>
Shows that the
<tt>major</tt>
interface takes one argument:
the number of the device whose associated major device number the
<tt>major</tt>
interface will obtain.
Upon successful completion,
<tt>major</tt>
returns the major number portion of the
<tt>dev_t</tt>
passed as the argument.
In this example, the major device number is stored in the
<tt><var>major_number</var></tt>
variable.
<a href="#co_id_63_rtn_2">[Return to example]</a>
</li></ol><p>
<a name="GetDevMinorNumb"></a>
</p><p></p><hr><p align="center">
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/Digital_UNIX_Bookshelf.html"><img src="char%20device%20driver%20interfaces_files/BOOKSHELF.GIF" alt="[Return to Library]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/TOC.html"><img src="char%20device%20driver%20interfaces_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#ImplCharDrvRoutines"><img src="char%20device%20driver%20interfaces_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#GetDevMajorNumb"><img src="char%20device%20driver%20interfaces_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#theuio_struct"><img src="char%20device%20driver%20interfaces_files/NEXT.GIF" alt="[Next Section]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut13.html"><img src="char%20device%20driver%20interfaces_files/FF.GIF" alt="[Next Chapter]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/INDEX.html"><img src="char%20device%20driver%20interfaces_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="char%20device%20driver%20interfaces_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
8.1.1.2&nbsp;&nbsp;&nbsp;&nbsp;Getting the Device Minor Number with minor
</h3>
<p>
To get the device minor number, call the
<tt>minor</tt>
interface.
The following code fragment shows a call to this interface:
</p><p>
</p><pre>xxread(dev, uio, flag)
dev_t dev; <a name="co_id_64_rtn_1"></a><a href="#co_id_64_1"><strong>[1]</strong></a>
struct uio *uio;
int flag;
<br>.<br>.<br>.<br>
int unit = minor(dev); <a name="co_id_64_rtn_2"></a><a href="#co_id_64_2"><strong>[2]</strong></a>
<br>
<br>.<br>.<br>.<br>
</pre>
<p>
</p><ol>
<p></p><li>
<a name="co_id_64_1"></a>
Declares an argument that specifies the major and minor numbers for a
specific
<tt>XX</tt>
device.
The major and minor number is passed to the
<tt>xxread</tt>
interface by the kernel as a result of an application's
<tt>read</tt>
system call.
The minor device number is used to determine the logical unit number for
the
<tt>XX</tt>
device on which the read operation is performed.
<a href="#co_id_64_rtn_1">[Return to example]</a>
<p></p></li><li>
<a name="co_id_64_2"></a>
<a name="nx_id_423"></a>
Shows that the
<tt>minor</tt>
interface takes one argument:
the number of the device whose associated minor device number the
<tt>minor</tt>
interface will obtain.
Upon successful completion,
<tt>minor</tt>
returns the minor number portion of the
<tt>dev_t</tt>
passed as the argument.
<a href="#co_id_64_rtn_2">[Return to example]</a>
</li></ol><p>
<a name="theuio_struct"></a>
</p><p></p><hr><p align="center">
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/Digital_UNIX_Bookshelf.html"><img src="char%20device%20driver%20interfaces_files/BOOKSHELF.GIF" alt="[Return to Library]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/TOC.html"><img src="char%20device%20driver%20interfaces_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#ImplCharDrvRoutines"><img src="char%20device%20driver%20interfaces_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#GetDevMinorNumb"><img src="char%20device%20driver%20interfaces_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#Theuio_iovMembers"><img src="char%20device%20driver%20interfaces_files/NEXT.GIF" alt="[Next Section]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut13.html"><img src="char%20device%20driver%20interfaces_files/FF.GIF" alt="[Next Chapter]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/INDEX.html"><img src="char%20device%20driver%20interfaces_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="char%20device%20driver%20interfaces_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
8.1.2&nbsp;&nbsp;&nbsp;&nbsp;Using the uio Structure
</h3>
<p>
<a name="nx_id_424"></a>
The
<tt>uio</tt>
structure describes I/O, either single vector or multiple vectors.
Typically, device drivers do not manipulate the members of this
structure.
However, the structure is presented here for the purpose of understanding the
<tt>uiomove</tt>
kernel interface, which operates on the members of the
<tt>uio</tt>
structure.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -