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

📄 char n block.htm

📁 What is this ``device driver stuff anyway? Here s a very short introduction to the concept.
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<html><head><title>Implementing Character and Block 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="ImplBlkandCharDrvRtns"></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%20n%20block_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%20n%20block_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut13.html"><img src="char%20n%20block_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<img src="char%20n%20block_files/BLANK.GIF" border="0">
<a href="#ImplementTheOpenRoutine"><img src="char%20n%20block_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/drivertut15.html"><img src="char%20n%20block_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%20n%20block_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%20n%20block_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h1>10&nbsp;&nbsp;&nbsp;&nbsp;Implementing Character and Block Device Driver Interfaces</h1>
<p>
Character and block device drivers can contain the following sections:
</p><ul>
<p></p><li>
An open and close device section, which
contains an
<tt>open</tt>
interface and a
<tt>close</tt>
interface
<p></p></li><li>
An
<tt>ioctl</tt>
section, which
contains an
<tt>ioctl</tt>
interface 
<p></p></li><li>
An interrupt section, which
contains an interrupt
handler 
</li></ul><p>
The following sections explain how to implement or set up each of these
interfaces.
<a name="ImplementTheOpenRoutine"></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%20n%20block_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%20n%20block_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#ImplBlkandCharDrvRtns"><img src="char%20n%20block_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<img src="char%20n%20block_files/BLANK.GIF" border="0">
<a href="#SetUpOpenRoutine"><img src="char%20n%20block_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/drivertut15.html"><img src="char%20n%20block_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%20n%20block_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%20n%20block_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h2>
10.1&nbsp;&nbsp;&nbsp;&nbsp;Implementing the open Interface
</h2>
<p>
<a name="nx_id_511"></a>
A device driver's
<tt>open</tt>
interface performs the tasks necessary to open a device and prepare it
for I/O operations.
The kernel calls the driver's
<tt>open</tt>
interface on behalf of applications that make an
<tt>open</tt>
system call on the device special file that corresponds to the driver.
</p><p>
The code associated with an
<tt>open</tt>
interface resides in the open and close device section of the device
driver.
<a name="nx_id_512"></a>
You specify the entry point for the driver's
<tt>open</tt>
interface
in a
<tt>dsent</tt>
structure.
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut8.html#dev_swit_tbls">Section 5.4</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
an
<tt>open</tt>
interface.
</p><p>
To implement an
<tt>open</tt>
interface you must understand the
<tt>dev_t</tt>
data type, which is discussed in
<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>.
The following list describes some typical tasks that you perform when
implementing
an
<tt>open</tt>
interface:
</p><p>
</p><ul>
<p></p><li>
Set up the
<tt>open</tt>
interface
<p></p></li><li>
Perform tasks associated with opening a device
</li></ul><p>
Your
<tt>open</tt>
interface will probably perform most of these tasks and, possibly, some
additional ones.
The following sections describe each of these tasks, using the
<tt>/dev/none</tt>
driver as an example.
<a name="SetUpOpenRoutine"></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%20n%20block_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%20n%20block_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#ImplBlkandCharDrvRtns"><img src="char%20n%20block_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#ImplementTheOpenRoutine"><img src="char%20n%20block_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#DetermDevOpen"><img src="char%20n%20block_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/drivertut15.html"><img src="char%20n%20block_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%20n%20block_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%20n%20block_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
10.1.1&nbsp;&nbsp;&nbsp;&nbsp;Setting Up the open Interface
</h3>
<p>
<a name="nx_id_513"></a>
The following code shows you how to set up an
<tt>open</tt>
interface, using the
<tt>/dev/none</tt>
driver as an example:
</p><p>
</p><p>
</p><pre><p>
noneopen(dev, flag, format)
dev_t dev;  <a name="co_id_80_rtn_1"></a><a href="#co_id_80_1"><strong>[1]</strong></a>
int flag;   <a name="co_id_80_rtn_2"></a><a href="#co_id_80_2"><strong>[2]</strong></a>
int format; <a name="co_id_80_rtn_3"></a><a href="#co_id_80_3"><strong>[3]</strong></a>
{
</p></pre>
<p>
<br>
<tt>.</tt>
<br>
<tt>.</tt>
<br>
<tt>.</tt>
<br>
</p><p>
</p><ol>
<p></p><li>
<a name="co_id_80_1"></a>
Declares an
argument that specifies the major and minor device numbers for
a specific
<tt>NONE</tt>
device.
The minor device number is used to determine the logical unit number for
the
<tt>NONE</tt>
device that is to be opened.
<a href="#co_id_80_rtn_1">[Return to example]</a>
<p></p></li><li>
<a name="co_id_80_2"></a>
Declares an argument to contain flag bits from the
file
<tt>/usr/sys/include/sys/file.h</tt>.
These flags indicate whether the device is being opened for reading,
writing, or both.
<a href="#co_id_80_rtn_2">[Return to example]</a>
<p></p></li><li>
<a name="co_id_80_3"></a>
Declares an argument to contain a constant that
identifies whether the device is a character or a block device.
These constants are defined in
<tt>/usr/sys/include/sys/mode.h</tt>.
<a href="#co_id_80_rtn_3">[Return to example]</a>
</li></ol><p>
<a name="DetermDevOpen"></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%20n%20block_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%20n%20block_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#ImplBlkandCharDrvRtns"><img src="char%20n%20block_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#SetUpOpenRoutine"><img src="char%20n%20block_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#ImplementTheCloseRoutine"><img src="char%20n%20block_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/drivertut15.html"><img src="char%20n%20block_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%20n%20block_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%20n%20block_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
10.1.2&nbsp;&nbsp;&nbsp;&nbsp;Performing the Tasks Associated with Opening the Device
</h3>
<p>
<a name="nx_id_514"></a>
The
<tt>/dev/none</tt>
driver's
<tt>open</tt>
interface (called
<tt>noneopen</tt>)
shows some of the typical tasks that a device driver's
<tt>open</tt>
interface can perform.
Specifically, the
<tt>/dev/none</tt>
driver's
<tt>open</tt>
interface performs the following tasks: checks to ensure that the
open is unique, marks the device as open, and returns
<tt>ESUCCESS</tt>
to indicate success.
The following code implements the
<tt>noneopen</tt>
interface.
The code also shows a call to the
<tt>minor</tt>
interface.
See
<cite>Writing Device Drivers: Advanced Topics</cite>
for an example implementation of an
<tt>open</tt>
interface for disk and tape device drivers.
</p><p>
<br>
<tt>.</tt>
<br>
<tt>.</tt>
<br>
<tt>.</tt>
<br>
</p><p>
</p><pre><p>
</p><p>
	register int unit = minor(dev);            <a name="co_id_81_rtn_1"></a><a href="#co_id_81_1"><strong>[1]</strong></a>
	struct controller *ctlr = noneinfo[unit];  <a name="co_id_81_rtn_2"></a><a href="#co_id_81_2"><strong>[2]</strong></a>
	struct none_softc *sc = &amp;none_softc[unit]; <a name="co_id_81_rtn_3"></a><a href="#co_id_81_3"><strong>[3]</strong></a>
</p><p>
</p><p>
	if(unit &gt;= num_none)
		return (ENODEV); <a name="co_id_81_rtn_4"></a><a href="#co_id_81_4"><strong>[4]</strong></a> 
</p><p>
</p><p>
	if (sc-&gt;sc_openf == DN_OPEN)
		return (EBUSY); <a name="co_id_81_rtn_5"></a><a href="#co_id_81_5"><strong>[5]</strong></a>
</p><p>
</p><p>
	if ((ctlr !=0) &amp;&amp; (ctlr-&gt;alive &amp; ALV_ALIVE))
 	{
		sc-&gt;sc_openf = DN_OPEN;
		return(ESUCCESS); <a name="co_id_81_rtn_6"></a><a href="#co_id_81_6"><strong>[6]</strong></a>
 	}
</p><p>
	else return(ENXIO); <a name="co_id_81_rtn_7"></a><a href="#co_id_81_7"><strong>[7]</strong></a> 
}
</p></pre>
<p>
</p><p>
</p><ol>
<p></p><li>
<a name="co_id_81_1"></a>
Declares a
<tt><var>unit</var></tt>
variable and initializes it to the device minor number.
Note the use of the
<tt>minor</tt>
interface to obtain the device minor number.
<p>
The
<tt>minor</tt>
interface takes one argument: the number of the device for which an
associated device minor number will be obtained.
The minor number is encoded in the
<tt><var>dev</var></tt>
argument, which is of type
<tt>dev_t</tt>.
<a name="nx_id_515"></a>
<a href="#co_id_81_rtn_1">[Return to example]</a>
</p><p></p></li><li>
<a name="co_id_81_2"></a>
Declares a pointer to a
<tt>controller</tt>
structure and calls it
<tt>ctlr</tt>.
The driver initializes
<tt>ctlr</tt>
to the
<tt>controller</tt>
structure associated with this
<tt>NONE</tt>
device.
The minor device number,
<tt><var>unit</var></tt>,
is used as an index into the array of
<tt>controller</tt>
structures to determine which
<tt>controller</tt>
structure is associated with this
<tt>NONE</tt>
device.
<a href="#co_id_81_rtn_2">[Return to example]</a>
<p></p></li><li>
<a name="co_id_81_3"></a>
Declares a pointer to a
<tt>none_softc</tt>
structure and calls it
<tt>sc</tt>.
The driver initializes
<tt>sc</tt>
to the address of the
<tt>none_softc</tt>
structure associated with this
<tt>NONE</tt>
device.
The minor device number,
<tt><var>unit</var></tt>,
is used as an index into the array of
<tt>none_softc</tt>
structures to determine which
<tt>none_softc</tt>
structure is associated with this
<tt>NONE</tt>
device.
<a href="#co_id_81_rtn_3">[Return to example]</a>
<p></p></li><li>
<a name="co_id_81_4"></a>
The
<tt>NONE</tt>
device requires no real work to open; therefore, the code could simply
ignore the call and return
<tt>ESUCCESS</tt>.
To demonstrate some of the checking that a real driver might perform,
the example provides code that checks to be sure that the device exists.
<p>
If the device minor number,
<tt><var>unit</var></tt>,
is greater than or equal to the number of devices configured by the
system, returns the error code
<tt>ENODEV</tt>,
which indicates there is no such device on the system.
This error code is defined in
<tt>/usr/sys/include/sys/errno.h</tt>.
</p><p>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut11.html#RegISIswithhandlerRtns">Section 7.1.6</a>
shows how the
<tt>noneprobe</tt>
interface increments the
<tt><var>num_none</var></tt>
variable.
<a href="#co_id_81_rtn_4">[Return to example]</a>
</p><p></p></li><li>

⌨️ 快捷键说明

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