2024年6月6日发(作者:)

android自定义驱动(第一篇驱动)

简介

功能总体自下而上的流程如下:

1、kernel层驱动实现,提供设备驱动节点文件

2、user层Hal抽象硬件接口定义和实现

3、HDIL接口定义,实现Binder服务端,提供给Framework层调用

4、Framework层JNI作为Binder客户端调用HDIL 接口,并提供AIDL接

口应用层使用

5、App应用层通过调用AIDL调用Framework提供的服务

案例环境如下:

手机设备:Nexus 6p

开发环境系统:Ubuntu14

开发环境:AOSP内核 Linux 3.10.73

Android源码版本:8.1

一、创建驱动项目

在内核源码目录/drivers/下,新建hello目录:

msm/drivers$ mkdir hello

msm是我内核源码目录

二、目录下创建hello.h文件

源码地址:

#ifndef _HELLO_ANDROID_H_ #define _HELLO_ANDROID_H_ #include

h> #include #define HELLO_DEVICE_NODE_NAME "hello" #d

efine HELLO_DEVICE_FILE_NAME "hello" #define HELLO_DEVICE_PROC_NAME "hell

o" #define HELLO_DEVICE_CLASS_NAME "hello" struct hello_android_dev{ int val;

// 代表寄存器,类型为int struct semaphore sem; // 信号量,用于同步操作 st

ruct cdev dev; // 内嵌的字符设备,这个Linux驱动程序自定义字符设备结构体的

标准方法 }; #endif

这个头文件定义了一些字符串常量宏,后面会使用到。这里定义了一个字符设备结

构体hello_android_dev,这里就是我们虚拟的硬件设备了,val成员变量就代表设

备里面的寄存器,它为int类型,sem成员变量是一个信号变量,是用于同步访问

寄存器val的,dev成员变量是一个内嵌的字符设备,这个Linux驱动程序自定义字

符设备结构体的标准方法。

三、添加hello.c文件

源码地址: 在hello目录中添加hello.c文件,这个是驱动程序的实现部分。驱动

程序的主要功能就是向上层提供访问设备的寄存器的值,包括读和写。这里提供了

三种访问寄存器的方法:

1.通过proc文件系统来访问。

2.通过传统的设备文件的方式来访问。

3.通过devfs文件系统来访问。

下面分段描述驱动程序的实现; 首先添加包含必要的头文件和定义三种访问设备

的方法:

#include #include #include #inclu

de #include #include #include

m/uaccess.h> #include #include "hello.h"

传统的设备文件访问方法 主要定义hello_open、hello_release、hello_read、

hello_write四个打开、释放、读写设备文件的方法;

/* 传统的设备文件操作方法 */ static int hello_open(struct inode* inode, struct file*

filp); static int hello_release(struct inode* inode, struct file* filp); static ssize_t hello_

read(struct file* filp, char __user *buf, size_t cout, loff_t* f_pos); static ssize_t hello_wr

ite(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos);

通过devfs文件系统访问方法 原理:这里将设备的寄存器val看成是设备的一个属

性,通过读写这个属性来对设备进行访问,主要是实现hello_val_show、

hello_val_store两个方法,同事定义了两个内部使用的访问val值的方法

_hello_get_val和_hello_set_val:

/* 访问设置属性方法*/ static ssize_t hello_val_show(struct device* dev, struct devic

e_attribute* attr, char* buf); static ssize_t hello_val_store(struct device* dev, struct d

evice_attribute* attr, const char* buf, size_t count);

通过proc文件系统访问方法 主要实现了hello_proc_read、hello_proc_write两个

方法,同时定义了在proc文件系统创建和删除文件的方法hello_create_proc和

hello_remove_proc:

/*读取设备寄存器val的值,保存在page缓存区中*/ static ssize_t hello_proc_read

(char* page, char** start, off_t off, int count, int* eof, void* data) { if(off > 0) { *

eof = 1; return 0; } return _hello_get_val(hello_dev, page); } /*把缓冲区的值

buff保存到设备寄存器val中去*/ static ssize_t hello_proc_write(struct file* filp, co

nst char __user *buff, unsigned long len, void* data) { int err = 0; char* page = N

ULL; if(len > _SIZE) { printk(KERN_ALERT"The buff is too large:%lu.n", le

n); return -EFAULT; } page = (char*)__get_free_page(GFP_KERNEL); if(!pa

ge) { printk(KERN_ALERT"Failed to alloc page.n"); return -ENOMEM; }

/*先把用户提供的缓冲区值拷贝到内核缓冲区去*/ if(copy_from_user(page, buff,

len)) { printk(KERN_ALERT"Failed to copy buff from user.n"); err = -EFAU

LT; goto out; } err = _hello_set_val(hello_dev, page, len); out: free_page((u

nsigned long)page); return err; } /*创建/proc/hello/文件*/ static void hello_creat

e_proc(void) { struct proc_dir_entry* entry; entry = proc_create(HELLO_DEVIC

E_PROC_NAME, 0644, 0, &hello_fops); /*entry = create_proc_entry(HELLO_DEVIC

E_PROC_NAME, 0, NULL); // 该函数已被替代 if(entry) { entry->owner = THIS

_MODULE; entry->read_proc = hello_proc_read; entry->write_proc = hello_p

roc_write; }*/ } /*删除/proc/hello文件*/ static void hello_remove_proc(void) {

remove_proc_entry(HELLO_DEVICE_PROC_NAME, NULL); }

定义模块的加载和卸载方法 这里主要是执行设备的注册和初始化操作: 创建和删

除设备文件节点

/* 模块加载方法*/ static int __init hello_init(void) { } /*模块卸载方法*/ static void _

_exit hello_exit(void) {}

四、添加配置文件

在hello目录创建Kconfig和Makefile两个文件,其中Kconfig是在编译前执行配

置命令make menuconfig时用到,而Makefile是执行编译命令make时用

到; Makefile文件内容:

obj-$(CONFIG_HELLO) += hello.o

Kconfig文件内容:

config HELLO tristate "First Android Driver" default m help This is the first

android driver.

说明:在Kconfig配置文件中,tristate表示编译选项HELLO支持在编译内核时,

hello模块支持以模块、内建和不编译三种编译方法,默认编译为模块,因此,在

编译内核前,我们需要使用make menuconfig命令配置编译选项,使得hello可以

以模块或者内建的方法进行编译。 在Makefile文件中,根据选择HELLO的值,执

行不同的编译方法。

五、添加hello模块项目到驱动程序中

修改drivers/Kconfig在menu "Device Drivers" 后添加一行:

source "drivers/hello/Kconfig"

将我们自定义的hello驱动模块引入; 修改drivers/Makefile,添加hello编译模

块在里面添加

obj-$(CONFIG_HELLO) += hello/

在内核目录/arch/arm64/configs/angler_defconfig中添加一行:

CONFIG_HELLO=y

这里m表示编译成module,y表示编译进kernel只可以看到.o不会生成.ko 这样,

执行make menuconfig时,就可以配置hello模块的编译选项了,配置为编译成模

块。.

六、配置编译选择

步骤如下:

1.内核目录下执行make menuconfig,弹窗选择框,进入Device Drivers-->选

择First Android Driver 按y键是选中,n是不选中;

2.注意: 如果内核不支持动态加载模块,这里不能选择m,虽然我们在

Kconfig文件中配置了HELLO选项为tristate。要支持动态加载模块选项,

必须要在配置菜单中选择Enable loadable module support选项;在支持动

态卸载模块选项,必须要在Enable loadable module support菜单项中,选

择Module unloading选项。 我们这里是支持动态加载的,因此选择m,方

便加载调试。

七、编译

开始编译内核 $ make kernel 编译到内核方式: 我这里是直接编译到内核的方式,剩

下步骤按执行,编译得到内核复制到AOSP源码编译生成镜像,然后刷

入到手机中验证; 模块方式: 获得,导出放到系统驱动目录,我的平台为:

/vendor/lib64/modules/ 使用insmod /vendor/lib64/modules/加载,为

了使驱动能在系统启动时加载,在中添加:insmod

/vendor/lib64/modules/,这样系统开机后就会启动了

八、验证

① 进入到dev目录,可以看到hello设备文件:

② 进入到proc目录,可以看到hello文件:

③进入到sys/class目录,可以看到hello目录:

以上都可以通过cat命令查看,echo '8' > hello命令写入操作