七、linux驱动注册

一、驱动注册结构体

        驱动注册使用结构体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);

 运行如下:当遇到驱动无法卸载时,请按照下面操作解决

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello Jason

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值