Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static functions in Linux device driver?

Is there a reason why most function definition in device driver in linux code is defined as static? Is there a reason for this?

I was told this is for scoping and to prevent namespace pollution, could anyone explain it in detail why static definition is used in this context?

like image 462
andycjw Avatar asked Dec 08 '08 09:12

andycjw


People also ask

What is init and exit functions of a driver?

__init and __exit attributes This section is known in advance to the kernel, and freed when the module is loaded and the init function finished. This applies only to built-in drivers, not to loadable modules. The kernel will run the init function of the driver for the first time during its boot sequence.

What are Linux device drivers?

The software that handles or manages a hardware controller is known as a device driver. The Linux kernel device drivers are, essentially, a shared library of privileged, memory resident, low level hardware handling routines. It is Linux's device drivers that handle the peculiarities of the devices they are managing.

What are the two types of drivers in Linux?

The Linux kernel supports two main types of USB drivers: drivers on a host system and drivers on a device.


1 Answers

Functions declared static are not visible outside the translation unit they are defined in (a translation unit is basically a .c file). If a function does not need to be called from outside the file, then it should be made static so as to not pollute the global namespace. This makes conflicts between names that are the same are less likely to happen. Exported symbols are usually indentified with some sort of subsystem tag, which further reduces scope for conflict.

Often, pointers to these functions end up in structs, so they are actually called from outside the file they are defined in, but not by their function name.

like image 79
camh Avatar answered Sep 27 '22 01:09

camh