Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


6.2.5 Registration


The kernel has a natural interest in knowing which character and block devices are available in the
system, thus a database needs to be maintained. Additionally, an interface for driver writers to submit
new entries into the database must be provided.

Data Structures


Let us turn our attention to the data structures used to manage devices.

The Device Database


Although block and character devices can and do behave very differently from each other, the databases
employed to keep track of all available devices are identical. This is natural since both block and character
devices are identified by a unique device number. Nevertheless, the database keeps track of different
objects dependent on whether block or character devices are managed.

❑ Each character device is represented by an instance ofstruct cdev.
❑ struct genhdis used to manage partitions of block devices and plays a role similar to that of
cdevfor character devices. This is reasonable since a block device without partitions can also be
seen as a block device with a single, large partition!

For now, it is sufficient to know that each block and character device is represented by an instance of
the respective data structure. Their contents do not matter here as we will have a closer look at them
below. Instead, it is important to see how the kernel keeps track of all availablecdevandgenhdinstances.
Figure 6-5 summarizes the situation graphically.

[b,c] dev_map

dev_
range
data

dev_
range
data

dev_
range

struct cdev
struct genhd

dep. on
device
type

data

Figure 6-5: Device database to keep track of all
block and character devices.

A global array —bdev_mapfor block andcdev_mapfor character devices — is used to implement a hash
table, which employs the device major number as hash key. Bothcdev_mapandbdev_mapare instances of
the same data structure,struct kobj_map. The hashing method is quite simple:major % 255.Thisworks
well since currently only a very limited number of devices has major numbers larger than 255, so hash
collisions are rare. The definition ofstruct kobj_mapalso includes the definition of the hash list elements
struct probe.
Free download pdf