您的位置:首页 > 编程语言

【平台设备驱动】中module_platform_driver的定义和使用

2017-02-08 17:21 866 查看
 

该函数实际是一个宏,它在include/linux/platform_device.h中定义如下:

[cpp] view
plain copy

 





/* module_platform_driver() - Helper macro for drivers that don't do 

 * anything special in module init/exit.  This eliminates a lot of 

 * boilerplate.  Each module may only use this macro once, and 

 * calling it replaces module_init() and module_exit() 

 */  

#define module_platform_driver(__platform_driver) \  

    module_driver(__platform_driver, platform_driver_register, \  

            platform_driver_unregister)  

其中的module_driver在/include/linux/device.h中定义,如下:

[cpp] view
plain copy

 





/** 

 * module_driver() - Helper macro for drivers that don't do anything 

 * special in module init/exit. This eliminates a lot of boilerplate. 

 * Each module may only use this macro once, and calling it replaces 

 * module_init() and module_exit(). 

 * 

 * @__driver: driver name 

 * @__register: register function for this driver type 

 * @__unregister: unregister function for this driver type 

 * @...: Additional arguments to be passed to __register and __unregister. 

 * 

 * Use this macro to construct bus specific macros for registering 

 * drivers, and do not use it on its own. 

 */  

#define module_driver(__driver, __register, __unregister, ...) \  

static int __init __driver##_init(void) \  

{ \  

    return __register(&(__driver) , ##__VA_ARGS__); \  

} \  

module_init(__driver##_init); \  

static void __exit __driver##_exit(void) \  

{ \  

    __unregister(&(__driver) , ##__VA_ARGS__); \  

} \  

module_exit(__driver##_exit);  

module_platform_driver(xxx);

最终展开后就是如下形式:

static int __init xxx_init(void)

{

        return platform_driver_register(&xxx);

}

module_init(xxx_init);

static void __exit
xxx_init(void)

{

        return platform_driver_unregister(&xxx);

}

module_exit(xxx_exit);

由上述定义可知,module_platform_driver()宏的作用就是定义指定名称的平台设备驱动注册函数和平台设备驱动注销函数,并且在函数体内分别通过platform_driver_register()函数和platform_driver_unregister()函数注册和注销该平台设备驱动。

参考文献:

1.Linux设备驱动工程师之路——platform类型按键驱动

2.Linux内核编程接口函数(alloc_chrdev_region/register_chrdev_region(字符设备)、class_create/device_create/device_create_file(设备节点)、module_platform_driver/container_of)

3.LINUX设备驱动之platform总线

4.platform设备驱动精讲,例程详细
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息