驱动注册
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("SKYFALL");
static int hello_probe(struct platform_device *pdv){
printk(KERN_EMERG "\tinitialized\n");
printk("%s,%d\n",__func__,__LINE__);
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");
printk("%s,%d\n",__func__,__LINE__);
DriverState = platform_driver_register(&hello_driver);
/*
驱动一旦注册成功,会与设备进行匹配,匹配成功是由
platform_match函数进行匹配,驱动调用probe初始化函数
*/
printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
return 0;
}
static void hello_exit(void)
{
printk(KERN_EMERG "HELLO WORLD exit!\n");
printk("%s,%d\n",__func__,__LINE__);
platform_driver_unregister(&hello_driver);
}
module_init(hello_init);
module_exit(hello_exit);
相应的Makefile
#!/bin/bash
$(warning KERNELRELEASE = $(KERNELRELEASE))
ifeq ($(KERNELRELEASE),)
#内核的源码路径, ?= 条件赋值, uname -r 得到内核版本号
KERNELDIR ?= /home/skyfall/iTOP4412/kernel/iTop4412_Kernel_3.0
# := 立即赋值, 得到当前的绝对路径
PWD := $(shell pwd)
# -C 切换工作路径, $(MAKE) = make
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*
.PHONY: modules clean
else
# 生成模块
obj-m := register_driver.o
endif
make编译,生成驱动文件
将其放入开发板进行测试。