在看alsa驱动代码时看到以下内容
module_platform_driver(xxx_machine_driver);
宏定义如下:
/* 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() - 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_machine_driver);的结果就是:
static int __init xxx_machine_driver_init(void)
{
return platform_driver_register(&(xxx_machine_driver));
}
module_init(xxx_machine_driver_init);
static void __exit xxx_machine_driver_exit(void)
{
platform_driver_unregister(&(xxx_machine_driver));
}
module_exit(xxx_machine_driver_exit);
展开后可知,是定义设备驱动的注册函数和注销函数,xxx_machine_driver既是platform_driver结构体对象,也是注册函数和注销函数名字的一部分,只需一个module_platform_driver宏就能替代大段代码,使用及其方便