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

📄 testing dd.htm

📁 What is this ``device driver stuff anyway? Here s a very short introduction to the concept.
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>Testing a Device Driver</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="TestDevDrv"></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="testing%20DD_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="testing%20DD_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut14.html"><img src="testing%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<img src="testing%20DD_files/BLANK.GIF" border="0">
<a href="#WrtngTestProg"><img src="testing%20DD_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/drivertut16.html"><img src="testing%20DD_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="testing%20DD_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="testing%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h1>11&nbsp;&nbsp;&nbsp;&nbsp;Testing a Device Driver</h1>
<p>
After developing your device driver, you should produce the single
binary module and then statically or dynamically configure it into the
kernel.
The chapters in
Part 4
explain how to accomplish these tasks.
</p><p>
After your driver is statically or dynamically configured into the
kernel, you should test it.
One way to test a device driver is to write a test program.
Implementing the test program involves the following tasks:
</p><ul>
<p></p><li>
Writing the test program
<p></p></li><li>
Expanding the test program
<p></p></li><li>
Tracking down problems in testing
</li></ul><p>
Each of these tasks is discussed in the following sections.
<a name="WrtngTestProg"></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="testing%20DD_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="testing%20DD_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#TestDevDrv"><img src="testing%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<img src="testing%20DD_files/BLANK.GIF" border="0">
<a href="#DecidRigorousTest"><img src="testing%20DD_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/drivertut16.html"><img src="testing%20DD_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="testing%20DD_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="testing%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h2>
11.1&nbsp;&nbsp;&nbsp;&nbsp;Writing the Test Program
</h2>
<p>
The test program for the
<tt>/dev/none</tt>
device driver performs the following tasks:
</p><ul>
<p></p><li>
Opens the
<tt>NONE</tt>
device for both reading and writing.
<p></p></li><li>
Gets the count of bytes written to the device.
<p></p></li><li>
Writes 100 bytes.
<p></p></li><li>
Gets the byte count a second time.
The byte count should be 100 more than the first time.
<p></p></li><li>
Resets the count to the value zero (0).
<p></p></li><li>
Gets the count again, to verify that the reset worked.
<br>
<p></p></li><li>
Tries to read 100 bytes.
The test program should not be able to read any because
<tt>/dev/none</tt>
returns EOF on a read.
</li></ul><p>
<br>
The test program follows:
</p><p>
</p><p>
</p><pre>/***************************************************
 * testdevnone.c - Test program for /dev/none      *
 *                 driver                          *
 ***************************************************/
/***************************************************
 *                                                 *
 * Author: Digital Equipment Corporation           *
 *                                                 *
 ***************************************************/
/***************************************************
 *         INCLUDE FILES                           *
 *                                                 *
 ***************************************************/
/***************************************************
 *                                                 *
 *                                                 *
 * Header files required by testdevnone.c          *
 *                                                 *
 *                                                 *
 ***************************************************/
<p>
</p><p>
</p><p>
#include &lt;stdio.h&gt;
#include &lt;sys/ioctl.h&gt; <a name="co_id_86_rtn_1"></a><a href="#co_id_86_1"><strong>[1]</strong></a>
#include &lt;sys/file.h&gt;
#include &lt;sys/limits.h&gt;
#include &lt;io/ESA100/nonereg.h&gt; <a name="co_id_86_rtn_2"></a><a href="#co_id_86_2"><strong>[2]</strong></a>
</p><p>
</p><p>
</p><p>
</p><p>
char buf[100]; <a name="co_id_86_rtn_3"></a><a href="#co_id_86_3"><strong>[3]</strong></a>
</p><p>
main()
{
	int d; <a name="co_id_86_rtn_4"></a><a href="#co_id_86_4"><strong>[4]</strong></a>
	int count; <a name="co_id_86_rtn_5"></a><a href="#co_id_86_5"><strong>[5]</strong></a>
</p><p>
	if((d = open("/dev/none",O_RDWR)) == -1) <a name="co_id_86_rtn_6"></a><a href="#co_id_86_6"><strong>[6]</strong></a>
	{
		perror("/dev/none");
                exit(1);
	}
</p><p>
	ioctl(d,DN_GETCOUNT,&amp;count); <a name="co_id_86_rtn_7"></a><a href="#co_id_86_7"><strong>[7]</strong></a>
	printf("saw %d bytes\n",count); <a name="co_id_86_rtn_8"></a><a href="#co_id_86_8"><strong>[8]</strong></a>
</p><p>
	write(d,&amp;buf[0],100); <a name="co_id_86_rtn_9"></a><a href="#co_id_86_9"><strong>[9]</strong></a>
	printf("wrote 100 bytes\n"); <a name="co_id_86_rtn_10"></a><a href="#co_id_86_10"><strong>[10]</strong></a>
</p><p>
	ioctl(d,DN_GETCOUNT,&amp;count); <a name="co_id_86_rtn_11"></a><a href="#co_id_86_11"><strong>[11]</strong></a>
	printf("saw %d bytes\n",count); <a name="co_id_86_rtn_12"></a><a href="#co_id_86_12"><strong>[12]</strong></a>
</p><p>
	ioctl(d,DN_CLRCOUNT,&amp;count); <a name="co_id_86_rtn_13"></a><a href="#co_id_86_13"><strong>[13]</strong></a>
	printf("set count\n"); <a name="co_id_86_rtn_14"></a><a href="#co_id_86_14"><strong>[14]</strong></a>
</p><p>
	ioctl(d,DN_GETCOUNT,&amp;count); <a name="co_id_86_rtn_15"></a><a href="#co_id_86_15"><strong>[15]</strong></a>
        printf("saw %d bytes\n",count);
</p><p>
	count = read(d,&amp;buf[0],100); <a name="co_id_86_rtn_16"></a><a href="#co_id_86_16"><strong>[16]</strong></a>
        printf("was able to read %d bytes\n",count); <a name="co_id_86_rtn_17"></a><a href="#co_id_86_17"><strong>[17]</strong></a>
</p><p>
<br>
	exit(0); <a name="co_id_86_rtn_18"></a><a href="#co_id_86_18"><strong>[18]</strong></a>
}
</p></pre>
<p>
</p><p>
</p><ol>
<p></p><li>
<a name="co_id_86_1"></a>
Includes the
<tt>ioctl.h</tt>
header file because the test program uses the
<tt>DN_GETCOUNT</tt>
and
<tt>DN_CLRCOUNT</tt>
<tt>ioctl</tt>
macros defined in the device register header file
<tt>nonereg.h</tt>.
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut8.html#DevDrvRegHeadFile">Section 5.1.2</a>
discusses these macros.
<a href="#co_id_86_rtn_1">[Return to example]</a>
<p></p></li><li>
<a name="co_id_86_2"></a>
Includes the device register header file, which is discussed in
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut8.html#DevDrvRegHeadFile">Section 5.1.2</a>.
The directory specification adheres to the directory created in
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut19.html#CreDirContainDrvProdFiles">Section 14.1.1</a>.
The directory specification you make here depends on where you put the
device register header file.
<a href="#co_id_86_rtn_2">[Return to example]</a>
<p></p></li><li>
<a name="co_id_86_3"></a>
Declares an array that contains 100 character elements.
This array is used to store the 100 bytes written by the
<tt>nonewrite</tt>
interface. 
<a href="#co_id_86_rtn_3">[Return to example]</a>
<p></p></li><li>
<a name="co_id_86_4"></a>
Declares a variable to contain the return from the
<tt>open</tt>
system call.
<a href="#co_id_86_rtn_4">[Return to example]</a>
<p></p></li><li>
<a name="co_id_86_5"></a>
Declares a variable to store the character count.
<a href="#co_id_86_rtn_5">[Return to example]</a>
<p></p></li><li>
<a name="co_id_86_6"></a>
Calls the
<tt>open</tt>
system call, which in this example opens the device for read/write
operations.
<p>
The example passes two arguments:
</p><ul>
<p></p><li>
The first argument is the pathname that identifies the file to be
opened.
In the example,
<tt>/dev/none</tt>
is passed.
This path causes the kernel to:
<p>
Look up
<tt>/dev/none</tt>
in the file system
</p><p>
Notice that
<tt>/dev/none</tt>
is a character-special device
</p><p>
Look up the driver entry for
<tt>/dev/none</tt>
in the
<tt>dsent</tt>
table
</p><p>
Locate the pointer to the
<tt>noneopen</tt>
interface that was put there when the
<tt>/dev/none</tt>
driver called the
<tt>devsw_add</tt>
interface to register the
<tt>noneopen</tt>
interface in the
<tt>dsent</tt>
table
</p><p>
Call the
<tt>noneopen</tt>
interface, passing to it a data structure that describes the device being
opened
</p><p></p></li><li>
The second argument is a flag to tell the kernel how to open the file.
In the example,
the constant
<tt>O_RDWR</tt>
is passed.
This constant tells the kernel to open
<tt>/dev/none</tt>
for reading and writing.
</li></ul><p>
</p><p>
If the
<tt>open</tt>
system call executes successfully, it opens the device and
returns a nonnegative value to the
test program.
However, if the system call fails, the kernel (in cooperation with the
driver's
<tt>noneopen</tt>
interface) will have set the global variable
<tt><var>errno</var></tt>
to a constant value that describes the error.
If an error occurs, the test program prints the error, using the
<tt>perror</tt>
interface, and quits.
<a href="#co_id_86_rtn_6">[Return to example]</a>
</p><p></p></li><li>
<a name="co_id_86_7"></a>
After the device is opened, gets the initial character count
that was written to it.
Although the count should be zero, it may not be if other users have
already written data to the device.
<p>
Remember that the
<tt>noneioctl</tt>
interface in the driver is used to read or clear the count of
characters written to
<tt>/dev/none</tt>.
Here, the
<tt>ioctl</tt>
system call is used to read the count of characters written to the device.
The kernel notices that the file associated with the descriptor
<tt><var>d</var></tt>
is actually a special file, looks up its
<tt>ioctl</tt>
interface in the
<tt>dsent</tt>
table, and invokes the driver's
<tt>noneioctl</tt>
interface.
The kernel copies the return value from
<tt>noneioctl</tt>
back to the
<tt><var>count</var></tt>
variable defined in the test program
because the bitmask for
<tt>DN_GETCOUNT</tt>
instructs it to do so.
<a href="#co_id_86_rtn_7">[Return to example]</a>
</p><p></p></li><li>
<a name="co_id_86_8"></a>
Calls the
<tt>printf</tt>
interface to display on the console terminal a message that indicates the
number of bytes. 

⌨️ 快捷键说明

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