Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


Registering Devices


Registering devices consists of two separate steps as Figure 6-22 shows: Initializing the data structures of
the device, and including it into the data structure network.

device_register

device_initialize

device_add

Figure 6-22: Code flow diagram
fordevice_register.

device_initializemainly adds the new device to the device subsystem bykobj_set_kset_s(dev,
devices_subsys).

device_addrequires a little more effort. First of all, the parent/child relationship specified via
device->parentis brought over to the generic kernel object hierarchy:

drivers/base/core.c
int device_add(struct device *dev)
{
struct device *parent = NULL;
...
parent = get_device(dev->parent);
kobj_parent = get_device_parent(dev, parent);
dev->kobj.parent = kobj_parent;
...

Registering the device in the devices subsystem requires a simple call tokobject_addsince subsystem
membership was already set indevice_initialize.

drivers/base/core.c
kobject_set_name(&dev->kobj, "%s", dev->bus_id);
error = kobject_add(&dev->kobj);
...

Afterwardbus_add_deviceadds links within sysfs — one in the bus directory that points to the device,
and one in the device directory which points to the bus subsystem.bus_attach_devicetries to autoprobe
the device. If a suitable driver can be found, the device is added tobus->klist_devices.Thedeviceis
also added to the child list of the parent (before that, the device knew its parent, but the parent did not
know the child).

drivers/base/core.c
error = bus_add_device(dev);
bus_attach_device(dev);
if (parent)
klist_add_tail(&dev->knode_parent, &parent->klist_children);
...
}
Free download pdf