module_platform_driver源码分析

龙弘济
2023-12-01

        在Linux设备驱动开发使用platform平台驱动模型过程中,在定义且初始化好platform_driver结构体变量以后,我们需要向 Linux 内核注册一个platform 驱动,Linux kernel中有两种方法,源码:include/linux/platform_device.h

方式一:

/*
 * use a macro to avoid include chaining to get THIS_MODULE
 */
#define platform_driver_register(drv) \
	__platform_driver_register(drv, THIS_MODULE)
extern int __platform_driver_register(struct platform_driver *,
					struct module *);

/**
 * __platform_driver_register - register a driver for platform-level devices
 * @drv: platform driver structure
 * @owner: owning module/driver
 */
int __platform_driver_register(struct platform_driver *drv,
				struct module *owner)
{
	drv->driver.owner = owner;
	drv->driver.bus = &platform_bus_type;
    
    /* 最终调用driver_register来注册drv->driver */
	return driver_register(&drv->driver);
}
EXPORT_SYMBOL_GPL(__platform_driver_register);


/**
 * platform_driver_unregister - unregister a driver for platform-level devices
 * @drv: platform driver structure
 */
void platform_driver_unregister(struct platform_driver *drv)
{
	driver_unregister(&drv->driver);
}
EXPORT_SYMBOL_GPL(platform_driver_unregister);

方式二:


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

        由以上展开可以看出,module_platform_driver是由module_driver封装而来,并填充了platform_driver_registerplatform_driver_unregister两个函数作为宏的参数传递给了 module_driver的宏,这样做的目的是统一了接口函数,使得所有驱动的注册、注销函数都能统一的管理。

        因此,展开 module_platform_driver(xxx_driver),第一步展开,调用宏module_driver(xxx_driver,platform_driver_register,platform_driver_unregister);第二步展开是展开宏module_driver,展开的结果是(在宏定义里,## 的作用是将连个参数连在一起, # 的作用是加上双引号):

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()实现了 驱动函数的注册和注销操作。

 类似资料: