香港做网站公司有哪些网站是怎么优化的
使用platform总线驱动模式编写Linux驱动时,需要注册platform_driver(用于跟.dts文件的platform_device匹配)。下面介绍2种常用注册platform_driver方法:
1、module_init()、module_exit()
/* 定义平台drv,通过.name来比较dev */
struct platform_driver xxx_drv = {.probe = xxx_probe,.remove = xxx_remove,.driver = {.name = "xxx_name", //与platform_device匹配}
};//注册platform_driver驱动
static int xxx_drv_init(void)
{platform_driver_register(&xxx_drv); //注册驱动,最终调用driver_register()return 0;
}//注销platform_driver驱动
static void xxx_drv_exit(void)
{platform_driver_unregister(&xxx_drv);
}module_init(xxx_drv_init); //注册驱动
module_exit(xxx_drv_exit); //注销驱动
最终调用 platform_driver_register() 去注册platform_driver驱动。
2、module_platform_driver 宏
注册Linux驱动时,可以使用 module_init()、module_exit() 等函数实现。LInux提供module_platform_driver() 宏可替代这些步骤,用于简化驱动程序的编写。
先看下module_platform_driver宏定义:
/*********** 路径:include/linux/platform_device.h **********/
#define module_platform_driver(__platform_driver) \module_driver(__platform_driver, platform_driver_register, \platform_driver_unregister)/*********** 路径: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会使用module_driver来填充platform_driver_register、
platform_driver_unregister。module_platform_driver宏展开就和 module_init()、module_exit()是相同的,最终调用 platform_driver_register() 注册驱动。
举例:
/* 定义平台drv,通过.name来比较dev */
struct platform_driver xxx_drv = {.probe = xxx_probe,.remove = xxx_remove,.driver = {.name = "xxx_name",}
};module_platform_driver(xxx_drv);