Home > System Tutorial > LINUX > body text

Detailed explanation of Linux device model (1)_Basic concepts

WBOY
Release: 2024-02-15 15:54:02
forward
1185 people have browsed it

1 Introduction

In the article "The Overall Architecture of the Linux Kernel", Wowo mentioned that the Linux kernel supports almost all hardware devices with different functions in the world, which is also an advantage of Linux. However, this also results in half of the code in the Linux kernel being device drivers. With the rapid upgrading of hardware devices, the amount of device driver code is also growing rapidly. In my opinion, this phenomenon destroys the concept of "simplicity is beautiful" and makes the Linux kernel very bloated, messy and difficult to maintain. However, Wowo also understands that this is not Linux's fault, because Linux is a macro kernel and must face device diversity and implement corresponding drivers.

In order to reduce the complexity of Linux driver development caused by device diversity and implement functions such as device hot-plugging and power management, the Linux kernel introduces the concept of device model (also called driver model). The device model classifies and summarizes hardware devices, and then abstracts a set of standard data structures and interfaces. In this way, driver development becomes straightforward and requires only filling in and implementing the data structures specified by the kernel.

This article will start from the basic concept of the device model, and gradually explain the implementation and use of the Linux device model through the analysis of the corresponding kernel code.

2. Basic concepts of Linux device model

2.1 Concepts of Bus, Class, Device and Device Driver

The following figure is an example of a common hardware topology for embedded systems:
Detailed explanation of Linux device model (1)_Basic concepts

Hardware topology describes three of the four important concepts in the Linux device model: Bus, Class and Device (the fourth is Device Driver, which will be discussed later).

Bus (bus): Linux believes (you can refer to the comments of struct bus_type in include/linux/device.h) that the bus is a channel for information exchange between the CPU and one or more devices. In order to facilitate the abstraction of the device model, all devices should be connected to the bus (whether it is the CPU internal bus, a virtual bus or a "platform Bus").

Class (classification): In the Linux device model, the concept of Class is very similar to Class in object-oriented programming. It mainly collects devices with similar functions or attributes, so that a set of devices can be abstracted. Data structures and interface functions shared between multiple devices. Therefore, drivers belonging to devices belonging to the same Class no longer need to repeatedly define these public resources and can directly inherit from the Class.

Device (device): Abstract all hardware devices in the system, describing its name, attributes, subordinate Bus, subordinate Class and other information.

Device Driver: The Linux device model uses Driver to abstract the driver of the hardware device, which includes interface implementations related to device initialization and power management. Driver development in the Linux kernel basically revolves around this abstraction (implementing the specified interface functions).

Note: What is Platform Bus?
There are a type of devices in computers that are directly connected to the CPU through their respective device controllers, and the CPU can access them (or access their controllers) through regular addressing operations. This connection method is not a bus connection in the traditional sense. However, the device model should be universal, so Linux creates a fictitious Platform Bus for these devices to connect to.

2.2 The core idea of ​​the device model

The core idea of ​​the Linux device model is (to achieve xxx purpose through xxx means):

\1. Use two data structures, Device (struct device) and Device Driver (struct device_driver), to describe the hardware device from the two perspectives of "what is it used for" and "how to use it" respectively. This unifies the format for writing device drivers and changes driver development from essay questions to fill-in-the-blanks, thus simplifying device driver development.

\2. Also use the two data structures of Device and Device Driver to realize plug-and-play (hot-plug) of hardware devices.
In the Linux kernel, as long as any Device and Device Driver have the same name, the kernel will execute the initialization function (probe) in the Device Driver structure, which will initialize the device and make it available.
For most hot-swappable devices, their Device Drivers always exist in the kernel. When the device is not plugged in, its Device structure does not exist, so its Driver does not perform initialization operations. When the device is inserted, the kernel will create a Device structure (with the same name as Driver), which will trigger the execution of Driver. This is the plug and play concept.

\3. Solve the dependencies between devices through the "Bus–>Device" type tree structure (see the legend in Chapter 2.1), and this dependency is particularly important in processes such as power on and off, power management, etc.
Imagine that a device is mounted on a bus. To start the device, the bus to which it is mounted must first be started. Obviously, if there are many devices in the system and the dependencies are very complex, neither the kernel nor the driver developers will be able to maintain this relationship.
The tree structure in the device model can automatically handle this dependency. Before starting a device, the kernel will check whether the device depends on other devices or buses. If it depends, it will check whether the dependent objects have been started. If not, it will start them first until the conditions for starting the device are met. What driver developers need to do is to inform the kernel of the device's dependencies when writing a device driver.

\4. Use the Class structure and introduce object-oriented concepts into the device model, which can abstract common features to the greatest extent, reduce duplication of labor in the driver development process, and reduce workload.

The above is the detailed content of Detailed explanation of Linux device model (1)_Basic concepts. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!