📄 char n block.htm
字号:
<a name="co_id_81_5"></a>
If the
<tt>sc_openf</tt>
member of the
<tt>sc</tt>
pointer is equal to
<tt>DN_OPEN</tt>,
returns the error code
<tt>EBUSY</tt>,
which indicates that the
<tt>NONE</tt>
device has already been opened.
This error code is defined in
<tt>/usr/sys/include/sys/errno.h</tt>.
This example test is used to ensure that this unit of the
driver can be opened only once at a time.
This type of open is referred to as an exclusive access open.
<a href="#co_id_81_rtn_5">[Return to example]</a>
<p></p></li><li>
<a name="co_id_81_6"></a>
If the
<tt>ctlr</tt>
pointer is not equal to 0 and the
<tt>alive</tt>
member of
<tt>ctlr</tt>
has the
<tt>ALV_ALIVE</tt>
bit set,
then the device exists.
If this is the case, the
<tt>noneopen</tt>
interface sets the
<tt>sc_openf</tt>
member of the
<tt>sc</tt>
pointer to the open bit
<tt>DN_OPEN</tt>
and returns
<tt>ESUCCESS</tt>
to indicate a successful open.
<a href="#co_id_81_rtn_6">[Return to example]</a>
<p></p></li><li>
<a name="co_id_81_7"></a>
If the device does not exist,
<tt>noneopen</tt>
returns the error code
<tt>ENXIO</tt>,
which indicates that the device does not exist.
This error code is defined in
<tt>/usr/sys/include/sys/errno.h</tt>.
<a href="#co_id_81_rtn_7">[Return to example]</a>
</li></ol><p>
<a name="ImplementTheCloseRoutine"></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="#DetermDevOpen"><img src="char%20n%20block_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#SetUpCloseRoutine"><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.2 Implementing the close Interface
</h2>
<p>
<a name="nx_id_516"></a>
The
<tt>open</tt>
interface is called every time that any user initiates an
action that invokes the
<tt>open</tt>
system call.
The
<tt>close</tt>
interface, however, is called only when the last user initiates
an action that closes the device.
The reason for this difference is to allow the driver to take some
special action when there is no work left to perform.
</p><p>
A device driver's
<tt>close</tt>
interface performs the tasks necessary to close a device that was
previously opened by the driver's
<tt>open</tt>
interface.
The kernel calls the driver's
<tt>close</tt>
interface on behalf of applications that make a
<tt>close</tt>
system call on the device special file that corresponds to the driver.
This action occurs when the last file descriptor that is open and
associated with this device is closed when the application calls the
<tt>close</tt>
system call.
</p><p>
The code associated with a
<tt>close</tt>
interface resides in the open and close device section of the device
driver.
<a name="nx_id_517"></a>
You specify the entry point for the driver's
<tt>close</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
a
<tt>close</tt>
interface.
</p><p>
To implement a
<tt>close</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
a
<tt>close</tt>
interface:
</p><ul>
<p></p><li>
Set up the
<tt>close</tt>
interface
<p></p></li><li>
Perform tasks associated with closing the device
</li></ul><p>
Your
<tt>close</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="SetUpCloseRoutine"></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="#ImplementTheCloseRoutine"><img src="char%20n%20block_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#PerformCloseDevTasks"><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.2.1 Setting Up the close Interface
</h3>
<p>
<a name="nx_id_518"></a>
The following code shows you how to set up a
<tt>close</tt>
interface, using the
<tt>/dev/none</tt>
driver as an example:
</p><p>
</p><p>
</p><pre><p>
noneclose(dev, flag, format)
dev_t dev; <a name="co_id_82_rtn_1"></a><a href="#co_id_82_1"><strong>[1]</strong></a>
int flag; <a name="co_id_82_rtn_2"></a><a href="#co_id_82_2"><strong>[2]</strong></a>
int format; <a name="co_id_82_rtn_3"></a><a href="#co_id_82_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_82_1"></a>
Like the
<tt>noneopen</tt>
interface, the
<tt>noneclose</tt>
interface declares an argument that specifies the major and minor
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 to be closed.
<a href="#co_id_82_rtn_1">[Return to example]</a>
<p></p></li><li>
<a name="co_id_82_2"></a>
Like the
<tt>noneopen</tt>
interface, the
<tt>noneclose</tt>
interface also declares an argument to contain flag bits from the file
<tt>/usr/sys/include/sys/file.h</tt>.
Typically, a driver's
<tt>close</tt>
interface does not make use of this argument.
<a href="#co_id_82_rtn_2">[Return to example]</a>
<p></p></li><li>
<a name="co_id_82_3"></a>
Although the
<tt><var>format</var></tt>
argument is shown here,
a driver's
<tt>close</tt>
interface does not typically make use of this argument.
<a href="#co_id_82_rtn_3">[Return to example]</a>
</li></ol><p>
<a name="PerformCloseDevTasks"></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="#SetUpCloseRoutine"><img src="char%20n%20block_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#ImplementTheioctlRoutine"><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.2.2 Performing the Tasks Associated with Closing the Device
</h3>
<p>
<a name="nx_id_519"></a>
The
<tt>/dev/none</tt>
driver's
<tt>close</tt>
interface (called
<tt>noneclose</tt>)
shows some of the typical tasks that a device driver's
<tt>close</tt>
interface can perform.
Specifically, the
<tt>/dev/none</tt>
driver's
<tt>close</tt>
interface
uses the same arguments as
<tt>noneopen</tt>,
gets the device minor number in the same way, and initializes the
<tt>controller</tt>
and
<tt>none_softc</tt>
structures identically.
The purpose of
<tt>noneclose</tt>
is to turn off the open flag for the specified
<tt>NONE</tt>
device.
The following code implements the
<tt>noneclose</tt>
interface.
The code also shows calls to the following interfaces:
<tt>write_io_port</tt>
and
<tt>mb</tt>.
See
<cite>Writing Device Drivers: Advanced Topics</cite>
for an example implementation of a
<tt>close</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_83_rtn_1"></a><a href="#co_id_83_1"><strong>[1]</strong></a>
struct controller *ctlr = noneinfo[unit]; <a name="co_id_83_rtn_2"></a><a href="#co_id_83_2"><strong>[2]</strong></a>
struct none_softc *sc = &none_softc[unit]; <a name="co_id_83_rtn_3"></a><a href="#co_id_83_3"><strong>[3]</strong></a>
register io_handle_t reg =
(io_handle_t) ctlr->addr; <a name="co_id_83_rtn_4"></a><a href="#co_id_83_4"><strong>[4]</strong></a>
</p><p>
</p><p>
write_io_port(reg + NONE_CSR, 4, 0, 0); <a name="co_id_83_rtn_5"></a><a href="#co_id_83_5"><strong>[5]</strong></a>
</p><p>
</p><p>
mb(); <a name="co_id_83_rtn_6"></a><a href="#co_id_83_6"><strong>[6]</strong></a>
</p><p>
</p><p>
sc->sc_openf = DN_CLOSE; <a name="co_id_83_rtn_7"></a><a href="#co_id_83_7"><strong>[7]</strong></a>
</p><p>
</p><p>
return(ESUCCESS); <a name="co_id_83_rtn_8"></a><a href="#co_id_83_8"><strong>[8]</strong></a>
}
</p></pre>
<p>
</p><ol>
<p></p><li>
<a name="co_id_83_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_520"></a>
<a href="#co_id_83_rtn_1">[Return to example]</a>
</p><p></p></li><li>
<a name="co_id_83_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_83_rtn_2">[Return to example]</a>
<p></p></li><li>
<a name="co_id_83_3"></a>
Declares a pointer to a
<tt>none_softc</tt>
structure and calls it
<tt>sc</tt>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -