Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


6.2 Access to Devices


Device special files(often referred to asdevice files) are used to access expansion devices. These files are not
associated with a data segment on hard disk or on any other storage medium but establish a link with a
device driver in order to support communication with the expansion device. As far as the application is
concerned, there is little difference between processing regular files and devicefiles. Both are handled by
exactly the same library functions. However, for more convenient handling, several additional commands
are provided to perform actions on device files that are not possible on regular files.

6.2.1 Device Files


Let us examine the approach adopted by reference to a modem attached to a serial interface. The name
of the corresponding device file is/dev/ttyS0. The device is identified not by means of its filename but
by means of the major and minor number of the file; these numbers are managed as special attributes in
the filesystem.

The same tools used to read and write regular files are employed to write data to or read results from a
device file. For instance,

wolfgang@meitner>echo "ATZ" > /dev/ttyS0

sends an initialization string to a modem connected to the first serial interface.

6.2.2 Character, Block, and Other Devices


The ways in which data are exchanged between peripherals and the system can be classified into several
categories. Some devices are better suited to character-oriented exchange because the volumes of data
transferred are low. Others are better able to handle data blocks with a fixed number of bytes. The kernel
makes a distinction between character and block devices. The former category includes serial interfaces
and text consoles, while the latter covers hard disks, CD-ROM devices, and the like.

IdentifyingDevice Files


Both the above types can be distinguished by reference to the properties of their device files. Let us look
at some members of the/devdirectory.

wolfgang@meitner>ls -l /dev/sd{a,b} /dev/ttyS{0,1}
brw-r----- 1 root disk 8, 0 2008-02-21 21:06 /dev/sda
brw-r----- 1 root disk 8, 16 2008-02-21 21:06 /dev/sdb
crw-rw---- 1 root uucp 4, 64 2007-09-21 21:12 ttyS0
crw-rw---- 1 root uucp 4, 65 2007-09-21 21:12 ttyS1

In many aspects, the above output is not different from that for regular files, particularly as concerns the
access permissions. However, there are two important differences.

❑ The letter preceding the access permissions is eitherborcto denoteblockandcharacterdevices,
respectively.
❑ Instead of the file size, two figures are given; these are themajor numberandminor number.
Together they form a unique number that allowsthe kernel to find the corresponding device
driver.
Free download pdf