Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


Since all registration functions discussed above manipulate the database data structures in a straightfor-
ward way, I do not bother to discuss their code explicitly.

In ancient times long long ago, the standard registration function for character devices used to be
register_chrdev. It is still supported for backward compatibility, and quite a number of drivers have
not been updated to the new interface as describedabove. New code, however, should not employ it!^5
The function does also not work for device numbers larger than 255.

Representation of Block Devices


Registering block devices requires only a single call toadd_disk. To describe the device properties, an
instance ofstruct genhdis required as a parameter; this structure is discussed in Section 6.5.1.

Earlier kernel versions required block devices to be registered usingregister_blkdev, which has the
following prototype:

<fs.h>
int register_blkdev(unsigned int major, const char *name);

nameis usually identical to the device filename, but can be any arbitrary valid string. Although it is not
necessary to call the function anymore today, it is still possible. The benefit is that the block device will
show up in/proc/devices.

6.3 Association with the Filesystem


With few exceptions, device files are handled by standard functions in the same way as regular files.
They are managed in the virtual filesystem discussed in Chapter 8. Both regular files and device files are
accessed via an absolutely identical interface.

6.3.1 Device File Elements in Inodes


Each file in the virtual file system is associated with just one inode that manages the file properties. Since
the inode data structure is very lengthy, I don’t reproduce it in full here but only include the elements
relevant to device drivers.

<fs.h>
struct inode {
...
dev_t i_rdev;
...
umode_t i_mode;
...
struct file_operations *i_fop;
...
union {
...

(^5) Whenregister_chrdevis used, no handling ofstruct cdevis necessary since this is automatically managed. The reason is
simple: Thecdevabstraction was not available in the kernel at the timeregister_chrdevwas designed, so old drivers cannot
know anything about it.

Free download pdf