?? kernel interfaces with dd.htm
字號(hào):
<html><head><title>Using Kernel Interfaces with Device Drivers</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="UsingKernRtnswithDrvs"></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="kernel%20interfaces%20with%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="kernel%20interfaces%20with%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/drivertut24.html"><img src="kernel%20interfaces%20with%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<img src="kernel%20interfaces%20with%20DD_files/BLANK.GIF" border="0">
<a href="#StringInter"><img src="kernel%20interfaces%20with%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/drivertut26.html"><img src="kernel%20interfaces%20with%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="kernel%20interfaces%20with%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="kernel%20interfaces%20with%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h1>18 Using Kernel Interfaces with Device Drivers</h1>
<p>
This chapter discusses the kernel interfaces most commonly used by
device drivers and provides code fragments to illustrate how to call these
interfaces in device drivers.
<a name="nx_id_794"></a>
These code fragments and associated descriptions supplement the
reference page descriptions for these and the other kernel
interfaces presented in
<cite>Writing Device Drivers: Reference</cite>.
Specifically, the chapter discusses the following:
<a name="nx_id_795"></a>
</p><ul>
<p></p><li>
String interfaces
<p></p></li><li>
Virtual memory interfaces
<p></p></li><li>
Data copying interfaces
<p></p></li><li>
Hardware-related interfaces
<p></p></li><li>
Kernel-related interfaces
<p></p></li><li>
Interfaces related to interrupt handler registration
<p></p></li><li>
Interfaces related to the I/O handle
<p></p></li><li>
Interfaces related to direct memory access
<p></p></li><li>
Miscellaneous interfaces
</li></ul><p>
<a name="StringInter"></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="kernel%20interfaces%20with%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="kernel%20interfaces%20with%20DD_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#UsingKernRtnswithDrvs"><img src="kernel%20interfaces%20with%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<img src="kernel%20interfaces%20with%20DD_files/BLANK.GIF" border="0">
<a href="#CompareTwoNullTermStr"><img src="kernel%20interfaces%20with%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/drivertut26.html"><img src="kernel%20interfaces%20with%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="kernel%20interfaces%20with%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="kernel%20interfaces%20with%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h2>
18.1 String Interfaces
</h2>
<p>
<a name="nx_id_796"></a>
String interfaces allow device drivers to:
</p><ul>
<p></p><li>
Compare two null-terminated strings
<p></p></li><li>
Compare two strings by using a specified number of characters
<p></p></li><li>
Copy a null-terminated character string
<p></p></li><li>
Copy a null-terminated character string with a specified limit
<p></p></li><li>
Return the number of characters in a null-terminated string
</li></ul><p>
The following sections describe the kernel interfaces that perform these tasks.
<a name="CompareTwoNullTermStr"></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="kernel%20interfaces%20with%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="kernel%20interfaces%20with%20DD_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#UsingKernRtnswithDrvs"><img src="kernel%20interfaces%20with%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#StringInter"><img src="kernel%20interfaces%20with%20DD_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#CompareTwoStrUsingChars"><img src="kernel%20interfaces%20with%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/drivertut26.html"><img src="kernel%20interfaces%20with%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="kernel%20interfaces%20with%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="kernel%20interfaces%20with%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
18.1.1 Comparing Two Null-Terminated Strings
</h3>
<p>
<a name="nx_id_797"></a>
<a name="nx_id_798"></a>
To compare two null-terminated character strings, call the
<tt>strcmp</tt>
interface.
The following code fragment shows a call to
<tt>strcmp</tt>:
</p><p>
</p><pre><br>.<br>.<br>.<br>
register struct device *device;
struct controller *ctlr;
<br>.<br>.<br>.<br>
if (strcmp(device->ctlr_name, ctlr->ctlr_name)) { <a name="co_id_127_rtn_1"></a><a href="#co_id_127_1"><strong>[1]</strong></a>
<br>.<br>.<br>.<br>
}
</pre>
<p>
</p><ol>
<p></p><li>
<a name="co_id_127_1"></a>
<a name="nx_id_799"></a>
Shows that the
<tt>strcmp</tt>
interface takes two arguments:
<ul>
<p></p><li>
The first argument specifies
a pointer to a string (an array of characters terminated by a
null character).
In this example, this is the controller name pointed to by the
<tt>ctlr_name</tt>
member of the pointer to the
<tt>device</tt>
structure.
<p></p></li><li>
The second argument also specifies
a pointer to a string (an array of characters terminated by a
null character).
In the example, this is the controller name pointed to by the
<tt>ctlr_name</tt>
member of the pointer to the
<tt>controller</tt>
structure.
</li></ul><p>
</p><p>
The code fragment sets up a condition statement that performs some tasks
based on the results of the comparison.
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut25.html#figResultsstrcmp">Figure 18-1</a>
shows how
<tt>strcmp</tt>
compares two sample character string values in the code fragment.
In item 1,
<tt>strcmp</tt>
compares the two controller names and returns the value zero (0)
because
<tt>strcmp</tt>
performed a lexicographical comparison between the two strings and they
were identical.
</p><p>
In item 2,
<tt>strcmp</tt>
returns an integer that is less than zero because the lexicographical
comparison indicates that the characters in the first controller name,
<tt>fb</tt>,
come before the letters in the second controller name,
<tt>ipi</tt>.
<a href="#co_id_127_rtn_1">[Return to example]</a>
</p></li></ol><p>
</p><p>
<a name="figResultsstrcmp"></a>
</p><h3>Figure 18-1: Results of the strcmp Interface</h3>
<p><img src="kernel%20interfaces%20with%20DD_files/zk-0624U.gif"></p><p>
<a name="nx_id_800"></a>
<a name="CompareTwoStrUsingChars"></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="kernel%20interfaces%20with%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="kernel%20interfaces%20with%20DD_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#UsingKernRtnswithDrvs"><img src="kernel%20interfaces%20with%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#CompareTwoNullTermStr"><img src="kernel%20interfaces%20with%20DD_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#CopyNullTermStr"><img src="kernel%20interfaces%20with%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/drivertut26.html"><img src="kernel%20interfaces%20with%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="kernel%20interfaces%20with%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="kernel%20interfaces%20with%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
18.1.2 Comparing Two Strings by Using a Specified Number of Characters
</h3>
<p>
<a name="nx_id_801"></a>
To compare two strings by using a specified number of characters, call the
<tt>strncmp</tt>
interface.
The following code fragment shows a call to
<tt>strncmp</tt>:
</p><p>
</p><pre><br>.<br>.<br>.<br>
register struct device *device;
<br>.<br>.<br>.<br>
if( (strncmp(device->dev_name, "rz", 2) == 0)) <a name="co_id_128_rtn_1"></a><a href="#co_id_128_1"><strong>[1]</strong></a>
<br>.<br>.<br>.<br>
</pre>
<p>
</p><ol>
<p></p><li>
<a name="co_id_128_1"></a>
<a name="nx_id_802"></a>
Shows that the
<tt>strncmp</tt>
interface takes three arguments:
<ul>
<p></p><li>
The first argument specifies a pointer to a string (an array of characters terminated by a
null character).
In the example, this is the device name pointed to by the
<tt>dev_name</tt>
member of the pointer to the
<tt>device</tt>
structure.
<p></p></li><li>
The second argument also specifies a pointer to a string (an array of characters terminated by a
null character).
In the example, this is the character string
<tt>rz</tt>.
<p></p></li><li>
The third argument specifies
the number of bytes to be compared.
In the example, the number of bytes to compare is 2.
</li></ul><p>
</p><p>
The code fragment sets up a condition statement that performs some tasks
based on the results of the comparison.
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut25.html#figResultsstrncmp">Figure 18-2</a>
shows how
<tt>strncmp</tt>
compares two sample character string values in the code fragment.
In item 1,
<tt>strncmp</tt>
compares the first two characters of the device name
<tt>none</tt>
with the string
<tt>rz</tt>
and returns an integer less than the value zero (0).
The reason for this is that
<tt>strncmp</tt>
makes a lexicographical comparison between the two strings and
the string
<tt>no</tt>
comes before the string
<tt>rz</tt>.
In item 2,
<tt>strncmp</tt>
compares the first two characters of the device name
<tt>rza</tt>
with the string
<tt>rz</tt>
and returns the value zero (0).
The reason for this is that
<tt>strncmp</tt>
makes a lexicographical comparison between the two strings and
the string
<tt>rz</tt>
is equal to the string
<tt>rz</tt>.
<a href="#co_id_128_rtn_1">[Return to example]</a>
</p></li></ol><p>
</p><p>
<a name="figResultsstrncmp"></a>
</p><h3>Figure 18-2: Results of the strncmp Interface</h3>
<p><img src="kernel%20interfaces%20with%20DD_files/zk-0568U.gif"></p><p>
<a name="nx_id_803"></a>
<a name="CopyNullTermStr"></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="kernel%20interfaces%20with%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="kernel%20interfaces%20with%20DD_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#UsingKernRtnswithDrvs"><img src="kernel%20interfaces%20with%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#CompareTwoStrUsingChars"><img src="kernel%20interfaces%20with%20DD_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#CopyNullTermStrSpecLimit"><img src="kernel%20interfaces%20with%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/drivertut26.html"><img src="kernel%20interfaces%20with%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="kernel%20interfaces%20with%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="kernel%20interfaces%20with%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
18.1.3 Copying a Null-Terminated Character String
</h3>
<p>
<a name="nx_id_804"></a>
<a name="nx_id_805"></a>
To copy a null-terminated character string, call the
<tt>strcpy</tt>
interface.
The following code fragment shows a call to
<tt>strcpy</tt>:
</p><p>
</p><pre><br>.<br>.<br>.<br>
struct tc_slot tc_slot[TC_IOSLOTS]; <a name="co_id_129_rtn_1"></a><a href="#co_id_129_1"><strong>[1]</strong></a>
char curr_module_name[TC_ROMNAMLEN + 1]; <a name="co_id_129_rtn_2"></a><a href="#co_id_129_2"><strong>[2]</strong></a>
<br>.<br>.<br>.<br>
strcpy(tc_slot[i].modulename, curr_module_name); <a name="co_id_129_rtn_3"></a><a href="#co_id_129_3"><strong>[3]</strong></a>
<br>.<br>.<br>.<br>
</pre>
<p>
</p><ol>
<p></p><li>
<a name="co_id_129_1"></a>
<a name="nx_id_806"></a>
Declares an array of
<tt>tc_slot</tt>
structures of size
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -