驱动注册
关键点
- 驱动注册结构体platform_driver 在 include/linux/platform_device.h中
- 驱动注册platform_driver_register,驱动卸载函数platform_driver_unregister 也在这个头文件中这两个函数的参数都只有结构体platform_deriver
- 驱动常见的状态有,初始化,移除,休眠,复位
- probe函数
–platform_match 函数匹配之后,驱动调用的初始化函数 - remove 函数
–移除驱动函数 - suspend函数
–悬挂(休眠)驱动函数 - resume函数
-休眠后恢复驱动 - device_driver 数据结构的两个参数
-name和注册设备的name 要一致
-owner一般赋值THIS_MODULE
源码
#defind DRIVER_NAME "hello_ctl";
#include <linux/init.h>
#include <linux/module.h>
// 驱动注册的头文件和设备注册的头文件和卸载函数
#include <linux/platform_device.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("xubaipei")
struct platform_driver_hello_driver={
.probe = hello_probe,
.remove = hello_remove,
.suspend = hello_suspend,
.shutdown = hello_shutdown,
.resume = hello_resume,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
}
};
static int hello_probe(struct platform_device *pdv)
{
printk(DRIVER_NAME,"\t initialized \n");
return 0;
}
static int hello_remove(struct platform_device *pdv)
{
return 0;
}
static int hello_suspend(struct platform_device *pdv)
{
return 0;
}
static void hello_shutdown(struct platform_device *pdv)
{
}
static int hello_resume(struct platform_device *pdv)
{
return 0;
}
module_init(hello_init);
module_exit(hello_exit);
static int hello_init(void)
{
int driverstate = -1;
printk(KERN_EMERG "HELLO_WORD init enter~\n");
driverstate = platform_driver_register(&hello_driver);
printk(DRIVER_NAME,"\t driver_state is:%d\n ",driverstate);
return 0;
}
static int hello_exit(void)
{
// 紧急权限打印,超级终端中查看
printk(KERN_EMERG "HELLO_WORD exit enter~\n");
platform_driver_unregister(&hello_driver);
return 0;
}
编译生成新的ko 文件
总结
设备必须提前注册,然后才是注册驱动,才能够执行probe 函数,且这两步是分开的,一般来说都是在平台文件中统一处理的