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

相关文章
|
Linux 网络架构
Linux 中查看本机的子网掩码和网关
Linux 中查看本机的子网掩码和网关
|
开发工具 数据安全/隐私保护 git
remote: The project you were looking for could not be found.
因为使用了不同的账号对不同的项目进行登录,在更换回原先的账户下载git上的代码时,就报了这个问题,这个问题的导致原因其实也就是我们不断更换登录账户所导致的。
1235 0
remote: The project you were looking for could not be found.
|
Ubuntu Linux 虚拟化
Linux下的IMX6ULL——构建bootloader、内核、文件系统(四)
Linux下的IMX6ULL——构建bootloader、内核、文件系统(四)
1458 0
Linux下的IMX6ULL——构建bootloader、内核、文件系统(四)
|
11月前
|
机器学习/深度学习 算法 自动驾驶
基于YOLOv8模型的行人车辆多目标检测计数与跟踪系统
本研究基于YOLOv8模型,针对智能交通与公共安全需求,开展行人车辆多目标检测、计数与跟踪技术研究。通过融合YOLOv8高精度检测与DeepSORT稳定跟踪,实现复杂场景下目标的实时定位、统计与轨迹追踪,提升交通管理效率与公共安全保障能力,推动智慧城市发展。
|
存储 人工智能 自然语言处理
构建AI智能体:三十七、从非结构化文本到结构化知识:基于AI的医疗知识图谱构建与探索
知识图谱是一种用图结构表示实体及其关系的技术,通过三元组(主体-关系-客体)构建语义网络。文章以医疗领域为例,详细介绍了知识图谱的构建流程:数据预处理、实体识别、关系抽取、知识融合、存储与可视化等步骤。知识图谱可应用于智能问答、辅助诊断、药物研发等场景,其结构化特性可弥补大语言模型的不足,二者结合能提升AI系统的准确性和可解释性。文章还展示了基于大模型的医疗知识图谱构建代码示例,涵盖实体识别、关系抽取、图谱存储和智能问答等核心功能,体现了知识图谱在专业领域的实用价值。
1973 12
|
Java Shell Linux
【Linux】shell编程基础(超详细,入门看这一篇就够了)(上)
【Linux】shell编程基础(超详细,入门看这一篇就够了)
1491 0
|
Ubuntu Linux
在Linux中,如何修改IP地址、网关和主机名?
在Linux中,如何修改IP地址、网关和主机名?
|
Linux 程序员
Linux系统中驱动之设备树的platform驱动实现
Linux系统中驱动之设备树的platform驱动实现
962 0
|
搜索推荐 关系型数据库 MySQL
/etc/profile,/etc/bashrc,~/.profile,~/.bashrc 的区别及使用
/etc/profile,/etc/bashrc,~/.profile,~/.bashrc 的区别及使用
851 0
|
传感器 监控 Linux
Android i2cdetect i2cdump i2cget i2cset调试工具使用
Android i2cdetect i2cdump i2cget i2cset调试工具使用
3793 1