Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


With thecdevinstance for the device in hand, the kernel also has access to the device-specific
file_operationsviacdev->ops. Various connections between data structures are then set up like
Figure 6-7.

struct inode

i_devices

i_cdev

p_list
ops

struct cdev struct file_operations

struct file

f_op

Figure 6-7: Relations between data structures for the representation of
character devices.

❑ inode->i_cdevpoints to the selectedcdevinstance. When the inode is opened next time, the
character device database need not be queriedanymorebecause the cached value can be used.
❑ The inode is added tocdev->list(i_devicesis used as the list element in the inode).
❑ file->f_ops,thatis,thefile_operationsforstruct file, are set to point to the
file_operationsinstance given bystruct cdev.

The (now device-specific)openmethod of the newfile_operationsfromstruct fileis then invoked
to carry out the required initialization tasks on the device (some peripherals need to negotiate operating
details by means of handshaking before they are used for the first time). The function can also be used to
make the data structure changes needed for a specific minor number.

Let us consider the example of a character device with major number 1. According to the LANANA
standard, this device has 10 different minor numbers; each provides a different function each provides a
different function, each of which relates to memory access operations. Table 6-1 lists a few minor numbers
together with the associated filenames and functions.

Some devices will be familiar, particularly the/dev/nulldevice. Without going into the details of the
individual minor numbers, it is clear from the device descriptions that there are considerable differences
between the functions implemented, even though they are all concerned with memory access. It is there-
fore not surprising that, again, only a single function pointer is defined in thefile_operationsstructure
of thechrdevsentry;openpoints tomemory_openafter one of the above files has been opened.

The procedure is defined indrivers/char/mem.cand implements a dispatcher that distinguishes
between the individual devices by reference to the minor number and selects the appropriate
file_operations. Figure 6-8 illustrates how the file operations change when a memory device is
opened.

The functions gradually reflect the special features of the device. Initially, only the general procedure
for opening character devices is known. This isthen replaced by a special procedure to open the
Free download pdf