嵌入式学习笔记(五)- 驱动注册

驱动注册

关键点

  • 驱动注册结构体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 函数,且这两步是分开的,一般来说都是在平台文件中统一处理的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值