1 Underlying mechanism the underlying mechanism of the Linux device driver model mainly includes several structures, such as kobject, kobj_type, and kset. These structures are defined in include/linux/kobject. h. 1.1kobject represents a basic object in the device driver model, similar to the top...
1 Underlying mechanism
The underlying mechanism of the device driver model in Linux mainly includes several structures, such as kobject, kobj_type, and kset. These
The structure is defined in include/linux/kobject. h.
1.1 kobject represents a basic object in the device driver model, similar to the top-level base class CObject in MFC. Each kobject corresponds to a directory in sysfs. The upper-layer structures such as device, device_driver, and bus_type are embedded with a kobject, which is equivalent to the inheritance mechanism in the object-oriented programming language.
1.2 kobj_type is the type to which kobject belongs. it defines the common attributes and operations of a certain type of kobejct.
1.3 kset is a collection of kobject (or containers) that contains a series of kobject. Note that the kset is also embedded with kobject, which indicates that the kset itself is also a kobject.
2. high-level data structure
The high-level layer consists of three structures: device, device_driver, and bus_type. These structures are defined in
Include/linux/device. h.
2.1 device represents a device, corresponding to a directory under/sys/devices/
2.2 device_driver represents a device driver, which corresponds to a directory under/sys/bus/XXX/drivers /.
2.3 bus_type indicates a bus, which corresponds to a directory under/sys/bus.
The 2.4 match () method is used to determine whether a device attached to the bus matches a certain device_driver.
It is called when device or a device_driver is registered on the bus. The uevnet () method is used to add environment variables for the uevent mechanism and is called when processing uevents. Other methods are to operate the mounted device on the bus.
2.5 probe (), remove (), shutdown (), suspend (), resume () are also set in device_driver.
. When the kernel calls these methods, it first calls the methods defined in bus_type. If the corresponding method is
If bus_type is not defined, the method defined in device_driver will be called.
3 Relationship between bus_type, device, and device_driver
3.1 bus_type is equivalent to a container. it is the management mechanism of device and device_driver. it contains a device set (kset) and a device_driver set (kset ), indicates all devices mounted on the bus and all device drivers.
3.2 device_driver is mounted under a bus_type and contains a device set (kset), which indicates that the driver operates (or controls) all devices. Device_driver also contains a bus_type pointer, indicating the bus where the driver is located.
3.3 The device is mounted under a bus_type and contains a device_driver pointer, indicating the device driver corresponding to the device. The device also contains a bus_type pointer, indicating the bus where the device is located. It should be noted that an actual bus represents in the device driver model in two structures: bus_type and device. Bus_type indicates the bus type and appears in the/sys/bus/directory; device indicates the bus device and appears in/sys/devices/
Directory, which indicates that the actual bus is essentially a device.
4. add and delete devices on the bus:
(1) add a device
Device_register (dev)-> device_add (dev)-> kobject_uevent (KOBJ_ADD)
Device_add (dev)-> bus_attach_device (dev)-> device_attach (dev)
->__ Device_attach (drv, dev)-> bus_type (): match (dev, drv)
Then-> driver_probe_device ()-> really_probe ()
-> Bus_type: probe () or device_driver: probe ()
(2) delete a device
Device_unregister (dev)-> device_del (dev)-> bus_remove_device (dev)
-> Device_release_driver (dev)
->__ Device_release_driver (dev)
-> Bus_type: remove () or device_driver: remove ()
Device_del (dev)-> kobject_uevent (KOBJ_REMOVE)
(3) when a device is added or deleted on the bus, all drivers on the bus are traversed in sequence and
The match () method checks whether the device to be added (or deleted) matches the driver. if the match succeeds
Call the probe (or remove) method defined in the bus or driver. When a device is added, KOBJ_ADD uevent is generated,
When a device is deleted, KOBJ_REMOVE uevent is generated.
5. summarize the process of adding and deleting drivers on the bus:
(1) add driver
Driver_register (drv)-> bus_add_driver (drv)-> driver_attach (drv)
->__ Driver_attach (dev, drv)
-> Bus_type (): match (dev, drv) then
-> Driver_probe_device ()-> really_probe ()
-> Bus_type: probe () or device_driver: probe ()
Driver_register (drv)-> kobject_uevent (KOBJ_ADD)
(2) driver deletion
Driver_unregister (dev)-> bus_remove_driver (drv)-> driver_detach (drv)
->__ Device_release_driver (dev) [all devices managed by dev-drv]
-> Bus_type: remove () or device_driver: remove ()
(3) when a driver is added or deleted on the bus, all devices on the bus are traversed in sequence and
The match () method checks whether the driver to be added (or deleted) matches the device. if the match succeeds
Call the probe (or remove) method defined in the bus or driver. Generate KOBJ_ADD when adding the driver
Uevent: KOBJ_REMOVE uevent is generated when the driver is deleted. [Note: KOBJ_REMOVE uevent
Generated in kobject_put (& drv-> p-> kobj)]