方法一:在Linux设备驱动开发过程中,在定义且初始化好platform_driver结构体变量以后,我们需要向 Linux 内核注册一个 platform 驱动。下面介绍两种方法。
int platform_driver_register(struct platform_driver *driver)
// driver:要注册的 platform 驱动。
// 返回值:负数,失败;0,成功。
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 文件中,如下:
#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 文件中,内容如下:
#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);
因此方法一就是方法二的展开形式。



