Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static functions in Linux device driver

Why is it that every function in most device drivers are static? As static functions are not visible outside of the file scope. Then, how do these driver function get called by user space applications?

like image 913
Yogesh R.L Avatar asked Jan 20 '13 09:01

Yogesh R.L


People also ask

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.

How do I run a device driver in Linux?

There are two ways of programming a Linux device driver: Compile the driver along with the kernel, which is monolithic in Linux. Implement the driver as a kernel module, in which case you won't need to recompile the kernel.

Where are device drivers stored in Linux?

Many Drivers come as part of the distribution's Kernel. Use Them. These Drivers are stored, as we saw, in the /lib/modules/ directory.


2 Answers

Remember than in C everything is addresses. That means you can call a function if you have the address. The kernel has a macro named EXPORT_SYMBOL that does just that. It exports the address of a function so that driver functions can be called without having to place header declarations since those functions are sometimes not know at compile time. In cases like this the static qualifier is just made to ensure that they are only called through this method and not from other files that may include that driver code (in some cases it's not a good idea to include driver code headers and call them directly).

EDIT: Since it was pointed out that I did not cover userspace.

Driver functions are usually not called through userspace directly (except for x86 implementation of SYSCALL instruction which does some little tricks to save the context switch sometimes). So the static keyword here makes no difference. It only makes a difference in kernel space. As pointed out by @Cong Wang, functions are usually place into a structure of function pointers so that they may be called by simply having structures point to this structure (such as file_ops, schedulers, filesystems, network code, etc...).

like image 179
Jesus Ramos Avatar answered Sep 28 '22 03:09

Jesus Ramos


Because these static function are not supposed to be used directly outside of the module. They are called by other functions in the module, among which can be the interface to an ioctl or whatever callbacks. This is why they can be called from user-space, they are just in the call path.

Take a look at the network dummy module:

dummy_dev_init() is obviously static:

static int dummy_dev_init(struct net_device *dev)
{
        dev->dstats = alloc_percpu(struct pcpu_dstats);
        if (!dev->dstats)
                return -ENOMEM;

        return 0;
}

but it is a callback of ->ndo_init() which is called when registering this network device.

static const struct net_device_ops dummy_netdev_ops = {
        .ndo_init               = dummy_dev_init,
        .ndo_uninit             = dummy_dev_uninit,
        .ndo_start_xmit         = dummy_xmit,
        .ndo_validate_addr      = eth_validate_addr,
        .ndo_set_rx_mode        = set_multicast_list,
        .ndo_set_mac_address    = eth_mac_addr,
        .ndo_get_stats64        = dummy_get_stats64,
        .ndo_change_carrier     = dummy_change_carrier,
};

And obvious no one should call dummy_dev_init() directly.

like image 42
Cong Wang Avatar answered Sep 28 '22 03:09

Cong Wang