Prior to version 2.6, the kernel did not have a unified model to obtain information about it. For this reason, a model has been developed for Linux devices, Linux Device Model. The main purpose of this model is to maintain internal data structures that reflect the state and structure of the system. Such information includes what devices exist in the system, in what state they are in terms of consumption management, to which bus they are attached, which drivers have associated, along with the structure of the buses, devices, drivers in the system.
To maintain this information, the kernel uses the following entities:
– device – a physical device that is attached to a bus
– driver – a software entity that can be associated with a device and perform operations with it
– bus (bus) – a device to which other devices can be attached
– class – a type of device that has a similar behavior; there is a class for disks, partitions, serial ports, etc.
– subsystem – a view on the structure of the system; The subsystems in the kernel include devices (devices – a hierarchical view on all the devices in the system), -magistrals (bus – a view of the devices depending on how they are attached to the buses), classes, etc.
The kernel provides a representation of its model in userspace through the virtual sysfs file system. It is usually mounted in the /sys directory and contains the following subdirectories:
– block – all block type devices available in the system (disks, partitions)
– bus – types of buses to which physical devices are connected (pci, ide, usb)
– class – classes of drivers that are available in the system (net, sound, usb)
– devices – the hierarchical structure of the devices connected in the system
– firmware – information obtained from the system firmware (ACPI)
– fs – information about the mounted file systems
– kernel – kernel status information (logged in users, hotplug)
– modules – the list of modules loaded at the current time
– power – information related to the power management subsystem
As you can see, there is a match between the kernel data structures within the described model and the subdirectories in the sysfs virtual file system. Although this similarity may lead to the two concepts being confused, they are different. The kernel device model may work without the sysfs file system, but the reciprocal is not true. The information in sysfs can be found in files containing one attribute.
Some standard attributes, represented by files or directories with the same name, are the following:
– dev – the major and minor identifier of the device; it can be used to create entries in the / dev directory automatically
– device – a symbolic link to the directory containing devices; it can be used to discover hardware devices that provide a particular service (for example, the PCI device of the eth0 network card)
– driver – a symbolic link to the directory containing drivers (located in /sys/bus/*/drivers)
The Linux Device Model offers a number of structures to ensure the interaction between a hardware device and a device driver. The whole model is based on struct object structures. With this structure, hierarchies are built and the following structures are implemented: struct bus_type, struct device and struct device_driver.
An object structure does not perform a single function. Such a structure is usually integrated into a larger structure. A object structure actually incorporates a set of features that will be offered to a higher abstraction level object in the Linux Device Model hierarchy. As you can see, the object structures are in a hierarchy: an object has a parent and has a kset member, which contains objects on the same level. Linux Device Model is used to allow straightforward association between system devices and drivers. Drivers can export information independent of the physical device from behind.