【Linux源码阅读】Linux 内核module_xxx_driver宏

  在阅读其他人编写的Linux驱动源码发现,最后驱动注册和注销时并未使用传统的“module_init”和“module_exit”,而是使用一个宏“module_xxx_driver”代替,如module_i2c_driver、module_spi_driver、module_platform_driver等等。


  Linux驱动程序模型是“总线驱动”+“设备驱动”,比如i2c总线和i2c设备,spi总线和spi设备。“module_xxx_driver”用于编写设备驱动程序调用,分析内核源码可知,其封装了设备的注册和注销函数定义及向内核注册该函数的功能。


module_i2c_driver

  以i2c总线为例。其函数宏名称为“module_i2c_driver”,原型位于“kernel/include/linux/i2c.h”中。

/**
 * module_i2c_driver() - Helper macro for registering a I2C driver
 * @__i2c_driver: i2c_driver struct
 *
 * Helper macro for I2C drivers which do not 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_i2c_driver(__i2c_driver) \
	module_driver(__i2c_driver, i2c_add_driver, \
			i2c_del_driver)

  “module_driver”也是一个宏,传入了i2c设备驱动注册和注销函数,宏原型位于“kernel/linux/device.h”中。

/**
 * 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);

注:
在宏定义define中
##:表示连接意思,如A##B—>AB
##_VA_ARGS_:可变参数宏,函数支持不定长参数

  假设驱动程序调用函数宏原型为:

module_i2c_driver(bmp180_driver);

  根据上述源码预编译后宏展开最终为:

static int __init bmp180_driver_init(void) 
{ 
     i2c_add_driver(&bmp180_driver);
     return 0; 
} 
  
static void __exit bmp180_driver_exit(void) 
{ 
     i2c_del_driver(&bmp180_driver);
} 
module_init(bmp180_driver_init); 
module_exit(bmp180_driver_exit); 

  这 恰恰是以往我们编写驱动时最后的设备注册和注销步骤。因此,我们可以直接调用“module_xxx_driver”实现设备注册、注销函数的定义及向内核注册。


  同理,spi、platform等总线也提供类似的实现宏。

spi:
module_spi_driver	 #/kernel/include/linux/spi/spi.h

platform:
module_platform_driver	 #/kernel/include/linux/platform_device.h
原创文章 128 获赞 147 访问量 36万+

猜你喜欢

转载自blog.csdn.net/qq_20553613/article/details/103789594
今日推荐