module_platform_driver()

在Linux设备驱动开发使用platform平台驱动模型过程中,在定义且初始化好platform_driver结构体变量以后,我们需要向 Linux 内核注册一个 platform 驱动。下面介绍两种方法。

方法一:

/**
* 在驱动入口函数里面调用platform_driver_register函数,platform_driver_register函数
* 原型如下所示:
*/
int platform_driver_register(struct platform_driver *driver)
// driver:要注册的 platform 驱动。
// 返回值:负数,失败;0,成功。
    
/**
*还需要在驱动卸载函数中通过platform_driver_unregister函数卸载platform驱动,
*platform_driver_unregister 函数原型如下:
*/
void platform_driver_unregister(struct platform_driver *drv)
// drv:要卸载的 platform 驱动。
// 返回值:无。
/**
* 具体驱动代码中实现如下所示
*/
    
// 驱动模块加载
static int __init xxx_init(void)
{
	return platform_driver_register(&xxx_driver);
}
module_init(xxx_init);
// 驱动模块卸载 
static void __exit xxx_exit(void)
{
	platform_driver_unregister(&xxx_driver);
}
module_exit(xxx_exit);

方法二:

除了方法一这种传统方法以外, Linux 内核中会大量采用 module_platform_driver 来完成向 Linux 内核注册 platform 驱动的操作。
module_platform_driver 定义在 include/linux/platform_device.h 文件中,如下:

/* 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_platform_driver 依赖 module_driver,module_driver 也是一个宏,定义在include/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);
/**
* 具体驱动代码中实现如下所示
*/
module_platform_driver(xxx_driver);

因此方法一就是方法二的展开形式。

  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铁头小哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值