Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


6.4 Character Device Operations


The hardware of character devices is usually very simple, and, not surprisingly, the associated drivers
are not very difficult to implement.

6.4.1 Representing Character Devices


Recall that character devices are represented bystruct cdev. We have seen how the kernel keeps a
database of all activecdevinstances, but have not yet looked into the content of the structure. It is defined
as follows:

<cdev.h>
struct cdev {
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};

kobjis a kernel object embedded in the structure. As usual, it is used for general management of the data
structure.ownerpoints to the module (if any) that provides the driver, andopsis a list of file operations
that implement specific operations to communicate with the hardware.devspecifies the device number,
andcountdenotes how many minors are associated with the device.listallows for implementing a list
of all inodes that represent device special files for the device.

Initially, the file operations for character devices are limited to just a single method for opening the
associated device file (always the first action when using a driver). Logically, let’s examine this method
first.

6.4.2 Opening Device Files


chrdev_openfromfs/devices.cis the generic function for opening character devices. Figure 6-6 shows
the associated code flow diagram.

chrdev_open

Find device specific file_operations

f_op->open

Figure 6-6: Code flow diagram forchrdev_open.

Assume that the inode that represents the device file has not been opened before. Given the device
number,kobject_lookupqueries the character device database introduced in Section 6.2.5 and returns
thekobjectinstance associated with the driver. This, in turn, allows for obtaining thecdevinstance.
Free download pdf