module_platform_driver源码分析

简介: module_platform_driver源码分析

module_platform_driver源码分析


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

相关文章
|
3月前
|
编译器 Windows
plugin cannot be loaded for module “QtQuick“ && Could not load the Qt platform plugin “windows“
本文讨论了在Qt应用程序中遇到的平台插件加载问题,包括具体的错误信息、解决方案和参考链接。问题表现为无法加载“QtQuick”模块的插件,并且无法找到“windows”平台插件。解决方案是修改环境变量`Qt5_DIR`以使用正确的Qt版本和编译器环境。
100 1
|
4月前
|
C++ Windows
vs2019 This application failed to start because it could not find or load the QT platform plugin
这篇文章介绍了在VS2019中解决QT程序运行时出现的“无法找到或加载QT平台插件”错误的步骤,通过将必要的DLL文件和插件目录复制到项目解决方案中解决了问题。
|
4月前
Electron——node_modules\ffi-napi\build\Release\ffi_bindings.node is not a valid Win32 application.
Electron——node_modules\ffi-napi\build\Release\ffi_bindings.node is not a valid Win32 application.
80 0
|
6月前
|
Windows
Qtdesigner报错:This application failed to stat could not find or load the Qt platform plugin “windows“
Qtdesigner报错:This application failed to stat could not find or load the Qt platform plugin “windows“
platform_get_resource=NULL内核源码分析
platform_get_resource=NULL内核源码分析
181 0
|
Linux 编译器
module_init源码分析
module_init源码分析
|
Linux
原因及解决办法:Failed to load module “canberra-gtk-module“
原因及解决办法:Failed to load module “canberra-gtk-module“
972 0
module_exit源码分析
module_exit源码分析

热门文章

最新文章