Static functions in Linux device driver

前端 未结 3 972
清歌不尽
清歌不尽 2020-12-24 10:08

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

3条回答
  •  不知归路
    2020-12-24 10:38

    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.

提交回复
热议问题