一、驱动注册结构体
驱动注册使用结构体platform_driver,该结构体在头文件“vim include/linux/platform_device.h”中,和刚刚那个设备注册的驱动在同一个头文件。
- 驱动注册platform_driver_register
- 驱动卸载函数platform_driver_unregister
也在这个头文件中,这两个函数的参数都只有结构体platform_driver
驱动常见的几种状态,初始化,移除,休眠,复位
就像PC一样,有的驱动休眠之后无法使用,有的可以使用;有的系统唤醒之后,驱动需要重新启动才能正常工作,也有直接就可以使用等等
• probe函数
– platform_match函数匹配之后,驱动调用的初始化函数
• remove函数
– 移除驱动函数
• suspend函数
– 悬挂(休眠)驱动函数
• resume函数
– 休眠后恢复驱动
• device_driver数据结构的两个参数
– name和注册的设备name要一致
– owner一般赋值THIS_MODULE
二、驱动源码
我们在前面最小驱动的代码基础上修改,主要实现platform_driver结构体中定义的函数,然后再通过platform_driver_register和platform_driver_unregister注册和卸载:
#include <linux/init.h>
#include <linux/module.h>
/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
#include <linux/platform_device.h>
#define DRIVER_NAME "hello_ctl"
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");
static int hello_probe(struct platform_device *pdv){
printk(KERN_EMERG "\tinitialized\n");
return 0;
}
static int hello_remove(struct platform_device *pdv){
return 0;
}
static void hello_shutdown(struct platform_device *pdv){
;
}
static int hello_suspend(struct platform_device *pdv){
return 0;
}
static int hello_resume(struct platform_device *pdv){
return 0;
}
struct platform_driver hello_driver = {
.probe = hello_probe,
.remove = hello_remove,
.shutdown = hello_shutdown,
.suspend = hello_suspend,
.resume = hello_resume,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
}
};
static int hello_init(void)
{
int DriverState;
printk(KERN_EMERG "HELLO WORLD enter!\n");
DriverState = platform_driver_register(&hello_driver); // 注册驱动
printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
return 0;
}
static void hello_exit(void)
{
printk(KERN_EMERG "HELLO WORLD exit!\n");
platform_driver_unregister(&hello_driver); // 注销驱动
}
module_init(hello_init);// 模块入口
module_exit(hello_exit);
运行如下:当遇到驱动无法卸载时,请按照下面操作解决