Get the App
SLTechnology News&Howtos  ›  Servers  › 

On the question of whether linux device drivers use global variables

Shulou Source: shulou.com Published: 2022-06-03 04:01:55 09月10日 Update

On the question of whether linux device drivers use global variables

Today, someone asked me whether linux device drivers use global variables, and why try not to use global variables?

In fact, this question is said from two aspects:

1) the use of global variables destroys the reentrancy of the function, and locks have to be used to protect global variables in order to achieve function reentry. This efficiency has become inefficient.

Function reentry, mainly the use of local variables, that is, variables on the thread's stack, each using their own, there will be no conflict. The other is to use locks to protect the integrity of the data.

2) using global variables violates the writing principle of linux drivers. That is, a driver should support driving multiple devices to work at the same time. For example, a motor driver may operate multiple motors at the same time, rotating in multiple axes, and one motor controls one axis, such as x-axis, y-axis, and z-axis. If global variables are used in the driver to store device information, it is impossible to support saving multiple device information at the same time. If you use a linked list, a linked list node holds a device information, although it is feasible, but every time the kernel operates on your driver's api, you have to traverse the list and compare object pointers to find the information about the device you operate with each time the kernel passes in the struct device (such as struct i2c_client, which contains struct device). Obviously inefficient.

So in fact, the right way is:

In probe, a private structure is assigned to hold the state information for this device. Then call the dev_set_drvdata series functions to bind your assigned structure to the struct device passed in during probe. The struct device is created by the bus driver when a new device is plugged in.

When the later system calls the api function of your driver, you pass in the struct device, and you retrieve your assigned private structure (containing the status information of the specific device, such as i2C address, etc.) through the dev_get_drvdata series function, so as to know which specific device you are operating.

For details, please refer to my "linux device driver Model" video.

Https://edu.51cto.com/course/17159.html

And my video "how to write a device driver" or "go deep into the linux kernel"

Https://edu.51cto.com/course/17132.html

Https://edu.51cto.com/course/17155.html

In addition, for my related training video, please see:

Welcome to all the courses I have released: https://edu.51cto.com/lecturer/8896847.html

Links to my new and more discounted packaged courses are as follows:

Https://edu.51cto.com/sd/0a9d4

Tags: Device driver variable global information function device driver multiple motor problem at the same time program structure video driver allocation kernel efficiency state course Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux Microsoft Apple Shulou Information