Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


6.4.3 Reading and Writing


The actual work of reading from and writing to character device files is not an especially interesting
task because of the links already established between the virtual file and the device driver code. Calling
the read and write operations of the standard library issues the system calls discussed in Chapter 8 to
ultimately invoke the relevant operations (primarilyreadandwrite)inthefile_operationsstructure.
The specific implementation of these methods varies according to device and cannot be generalized.

The above memory devices have it easy because theyneed not bother with interaction with concrete
peripherals but simply invoke other kernel functions to do their work for them.

For example, the/dev/nulldevice uses theread_nullandwrite_nullprocedures to implement read
and write operations on the bit bucket. A quick look at the kernel sources will confirm that the imple-
mentation of these functions is reallyverysimple.

drivers/char/mem.c
static ssize_t read_null(struct file * file, char __user * buf,
size_t count, loff_t *ppos)
{
return 0;
}

static ssize_t write_null(struct file * file, const char __user * buf,
size_t count, loff_t *ppos)
{
return count;
}

Reading from the null device returns nothing, and this is easy to implement; the result returned is a data
stream with a length of 0 bytes. Data written to the device are simply ignored, but a successful write
operation is reported for data of any length.

More complicated character devices supply functionsthat read and write meaningful results. The generic
mechanism, however, is unchanged.

6.5 Block Device Operations


Block devices account for the second large group of peripherals supported via the VFS interface of the
kernel. Unfortunately, the situation faced by block device drivers is more complicated than that for
character devices. This is caused by a range of circumstances, above all by the need for continuous speed
adjustment occasioned by the design of the block device layer, by the way in which block devices work,
and by the historical development of the block device layer.

Block devices differ fundamentally from character devices in three principle areas.

❑ Access can be performed at any point within the data. This can but need not be the case with
character devices.
❑ Data are always transferred in fixed-size blocks. Even if a single byte is requested, the device
driver must always fetch a complete block from the device. In contrast, character devices are
able to return individual bytes.
Free download pdf