驱动注册

platform_driver_register 函数和platform_driver_unregister 函数用于注册和卸载驱动。

在Linux 源码目录下,打开如下位置文件可以查看到: vim include/linux/platform_device.h

 

 

注册驱动的函数:

extern int platform_driver_register(struct platform_driver *)

卸载驱动的函数:

extern void platform_driver_unregister(struct platform_driver *)

 

 

platform_driver结构体也是定义在  include/linux/platform_device.h 头文件中。

 

 

该结构中包含了一组操作函数和一个struct device_driver 的对象。在驱动中首先要做的

就是定义platform_driver 中的函数,并创建这个结构的一个对象实例, 然后在init()函数中调用

platform_driver_register()向系统注册驱动。

 

函数int (*probe)(struct platform_device *);

主要是进行设备的探测和初始化。例如想调用一个GPIO,那么首先需要探测这个GPIO 是

否被占用了,如果被占用了那么初始化失败,驱动注册也就失败了;如果没有被占用,那么就

申明要占用它。

 

函数int (*remove)(struct platform_device *);

移除驱动,该函数中一般用于去掉设备节点或者释放软硬件资源。

 

 

结构体struct device_driver driver;

主要包含两个参数,一个是name 参数,驱动名称(需要和设备驱动结构体中的name 参

数一样);一个是owner,一般是THIS_MODULE。

 

测试代码示例:

#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 __init hello_init(void){
		int driver_status = 0;
		printk(KERN_EMERG "\tinitialized\n");
		driver_status = platform_driver_register(&hello_driver);
		printk(KERN_EMERG "\tdriver_status is %d\n",driver_status);

		return 0;
}

static void hello_exit(void){
		printk(KERN_EMERG "\tHello World Exit\n");
		platform_driver_unregister(&hello_driver);
		return ;	
}

module_init(hello_init);
module_exit(hello_exit);






















编写Makefie,进行编译,生成驱动文件。

all:
	make -C /home/skyfall/itop_kernel/iTop4412_Kernel_3.0 M=$(shell pwd) modules
clean:
	make -C /home/skyfall/itop_kernel/iTop4412_Kernel_3.0 M=$(shell pwd) clean
obj-m:=sample.o

将其放在开发板上进行测试。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值