Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


drivers/base/map.c
struct kobj_map {
struct probe {
struct probe *next;
dev_t dev;
unsigned long range;
struct module *owner;
kobj_probe_t *get;
...
void *data;
} *probes[255];
struct mutex *lock;
};

The mutexlockserializes access to the hash table. The elements ofstruct probeare as follows:

❑ nextconnects all hash elements in a singly linked list.
❑ devdenotes the device number. Recall that both major and minor numbers are contained in this
datum.
❑ The consecutive range of minor numbers is stored inrange. The minor numbers associated to
the device are thus given byMINORS(DEV) + range - 1.
❑ ownerpoints to the module (if any) providing the device driver.
❑ getpoints to a function that returns thekobjectinstance associated with the device. Usually,
this task is rather straightforward, but it maybecome more involved if device mappers are in
use.
❑ The distinction between block and character devices is made bydata. For character devices, it
points to an instance ofstruct cdev, whilestruct genhdis the pointer destination for block
devices.

Character Device Range Database


A second database is for character devices only. It is used to manage device number range allocation to
drivers. A driver can either request a dynamic device number, or it can specify a range that is supposed
to be acquired. In the first case, the kernel needs to find a free range, while in the second case, it must be
ensured that the desired range does not overlap with any existing region.

Again a hash table is employed to keep track of previously allocated device number ranges, and again
the major number is used as hash key. The data structure in question looks as follows:

fs/char_dev.c
static struct char_device_struct {
struct char_device_struct *next;
unsigned int major;
unsigned int baseminor;
int minorct;
char name[64];
struct file_operations *fops;
struct cdev *cdev; /* will die */
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
Free download pdf