📄 introduction.htm
字号:
<html><head><title>What is a Device Driver?</title>
<link rel="owner" href="mailto:">
<script language="JavaScript">
<!-- hide this
function help(message) {
self.status = message;
return true;
}
// stop hiding -->
</script></head>
<body>
<strong>The
HyperNews <a href="http://tldp.org/LDP/khg/HyperNews/get/khg.html">Linux KHG</a>
Discussion Pages</strong>
<hr>
<h3>What is a Device Driver?</h3>
<p>Making hardware work is tedious. To write to a hard disk,
for example, requires that you write magic numbers in magic
places, wait for the hard drive to say that it is ready to
receive data, and then feed it the data it wants, very
carefully. To write to a floppy disk is even harder, and
requires that the program supervise the floppy disk drive
almost constantly while it is running.
</p><p>Instead of putting code in each application you write to
control each device, you share the code between applications.
To make sure that that code is not compromised, you protect it
from users and normal programs that use it. If you do it
right, you will be able to add and remove devices from your
system without changing your applications at all. Furthermore,
you need to be able to load your program into memory and run
it, which the operating system also does. So an operating
system is essentially a priviledged, general, sharable library
of low-level hardware and memory and process control functions
and routines.
</p><p>All versions of Unix have an abstract way of reading and
writing devices. By making the devices act as much as possible
like regular files, the same calls (<tt>read()</tt>,
<tt>write()</tt>, etc.) can be used for devices and files.
Within the kernel, there are a set of functions, registered
with the filesystem, which are called to handle requests to do
I/O on ``device special files,'' which are those which
represent devices. (See <tt>mknod(1,2)</tt> for an explanation
of how to make these files.)
</p><p>All devices controlled by the same device driver are given
the same <b>major number</b>, and of those with the same major
number, different devices are distinguished by different
<b>minor numbers</b>. (This is not strictly true, but it is
close enough. If you understand where it is not true, you
don't need to read this section, and if you don't but want to
learn, read the code for the tty devices, which uses up 2 major
numbers, and may use a third and possibly fourth by the time
you read this. Also, the ``misc'' major device supports many
minor devices that only need a few minor numbers; we'll get to
that later.)
</p><p>This chapter explains how to write any type of Linux device
driver that you might need to, including character, block,
SCSI, and network drivers. It
explains what functions you need to write, how to initialize
your drivers and obtain memory for them efficiently, and what
function are built in to Linux to make your job easier.
</p><p>Creating device drivers for Linux is easier than you might
think. It merely involves writing a few functions and
registering them with the Virtual Filesystem Switch (VFS), so
that when the proper device special files are accessed, the VFS
can call your functions.
</p><p>However, a word of warning is due here: Writing a device
driver <b>is</b> writing a part of the Linux kernel. This
means that your driver runs with kernel permissions, and can do
anything it wants to: write to any memory, reformat your hard
drive, damage your monitor or video card, or even break your
dishes, if your dishwasher is controlled by your computer. Be
careful.
</p><p>Also, your driver will run in kernel mode, and the Linux
kernel, like most Unix kernels, is non-pre-emptible. This
means that if you driver takes a long time to work without
giving other programs a chance to work, your computer will
appear to ``freeze'' when your driver is running. Normal
user-mode pre-emptive scheduling does not apply to your driver.
</p><p>Copyright (C) 1992, 1993, 1994, 1996 Michael K. Johnson,
johnsonm@redhat.com.<br>
</p><p>
</p><p></p><hr size="3">
<p><b><a name="Messages">Messages</a></b>
<nobr>
<font size="-1">
</font>
</nobr>
</p><p>
<nobr>
<dl compact="compact">
<dt> 1. <img src="introduction_files/note.gif" alt="None:" align="middle" height="15" width="15">
<a href="http://tldp.org/LDP/khg/HyperNews/get/devices/whatis/1.html">
Question ?</a> <i> by Rose Merone</i> </dt>
<dt> -> <img src="introduction_files/news.gif" alt="News:" align="middle" height="15" width="15">
<a href="http://tldp.org/LDP/khg/HyperNews/get/devices/whatis/1/1.html">
Not yet...</a> <i> by <a href="http://tldp.org/LDP/khg/%7Ejohnsonm">Michael K. Johnson</a></i> </dt>
</dl>
</nobr>
</p><p>
</p><p>
<br>
<br></p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -