当前位置: 移动技术网 > 移动技术>移动开发>Android > Ubuntu中为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序

Ubuntu中为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序

2019年07月24日  | 移动技术网移动技术  | 我要评论

       在ubuntu android简单介绍硬件抽象层(hal)一文中,我们简要介绍了在android系统为为硬件编写驱动程序的方法。简单来说,硬件驱动程序一方面分布在linux内核中,另一方面分布在用户空间的硬件抽象层中。接着ubuntu android系统上编写linux内核驱动程序实现方法一文中举例子说明了如何在linux内核编写驱动程序。在这一篇文章中,我们将继续介绍android系统硬件驱动程序的另一方面实现,即如何在硬件抽象层中增加硬件模块来和内核驱动程序交互。在这篇文章中,我们还将学习到如何在android系统创建设备文件时用类似linux的udev规则修改设备文件模式的方法。

      一. 参照在ubuntu android系统上编写linux内核驱动程序实现方法一文所示,准备好示例内核驱动序。完成这个内核驱动程序后,便可以在android系统中得到三个文件,分别是/dev/hello、/sys/class/hello/hello/val和/proc/hello。在本文中,我们将通过设备文件/dev/hello来连接硬件抽象层模块和linux内核驱动程序模块。

      二. 进入到在hardware/libhardware/include/hardware目录,新建hello.h文件:

      user-name@machine-name:~/android$ cd hardware/libhardware/include/hardware

      user-name@machine-name:~/android/hardware/libhardware/include/hardware$ vi hello.h

      hello.h文件的内容如下:

#ifndef android_hello_interface_h
#define android_hello_interface_h
#include <hardware/hardware.h>

__begin_decls

/*定义模块id*/
#define hello_hardware_module_id "hello"

/*硬件模块结构体*/
struct hello_module_t {
	struct hw_module_t common;
};

/*硬件接口结构体*/
struct hello_device_t {
	struct hw_device_t common;
	int fd;
	int (*set_val)(struct hello_device_t* dev, int val);
	int (*get_val)(struct hello_device_t* dev, int* val);
};

__end_decls

#endif

 这里按照android硬件抽象层规范的要求,分别定义模块id、模块结构体以及硬件接口结构体。在硬件接口结构体中,fd表示设备文件描述符,对应我们将要处理的设备文件"/dev/hello",set_val和get_val为该hal对上提供的函数接口。

      三. 进入到hardware/libhardware/modules目录,新建hello目录,并添加hello.c文件。 hello.c的内容较多,我们分段来看。

      首先是包含相关头文件和定义相关结构:

#define log_tag "hellostub"

#include <hardware/hardware.h>
#include <hardware/hello.h>
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <cutils/atomic.h>

#define device_name "/dev/hello"
#define module_name "hello"
#define module_author "shyluo@gmail.com"

/*设备打开和关闭接口*/
static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);
static int hello_device_close(struct hw_device_t* device);

/*设备访问接口*/
static int hello_set_val(struct hello_device_t* dev, int val);
static int hello_get_val(struct hello_device_t* dev, int* val);

/*模块方法表*/
static struct hw_module_methods_t hello_module_methods = {
	open: hello_device_open
};

/*模块实例变量*/
struct hello_module_t hal_module_info_sym = {
	common: {
		tag: hardware_module_tag,
		version_major: 1,
		version_minor: 0,
		id: hello_hardware_module_id,
		name: module_name,
		author: module_author,
		methods: &hello_module_methods,
	}
};

  这里,实例变量名必须为hal_module_info_sym,tag也必须为hardware_module_tag,这是android硬件抽象层规范规定的。

      定义hello_device_open函数:

static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {
	struct hello_device_t* dev;dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));
	
	if(!dev) {
		loge("hello stub: failed to alloc space");
		return -efault;
	}

	memset(dev, 0, sizeof(struct hello_device_t));
	dev->common.tag = hardware_device_tag;
	dev->common.version = 0;
	dev->common.module = (hw_module_t*)module;
	dev->common.close = hello_device_close;
	dev->set_val = hello_set_val;dev->get_val = hello_get_val;

	if((dev->fd = open(device_name, o_rdwr)) == -1) {
		loge("hello stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);
		return -efault;
	}

	*device = &(dev->common);
	logi("hello stub: open /dev/hello successfully.");

	return 0;

 device_name定义为"/dev/hello"。由于设备文件是在内核驱动里面通过device_create创建的,而device_create创建的设备文件默认只有root用户可读写,而hello_device_open一般是由上层app来调用的,这些app一般不具有root权限,这时候就导致打开设备文件失败:

      hello stub: failed to open /dev/hello -- permission denied.   

       解决办法是类似于linux的udev规则,打开android源代码工程目录下,进入到system/core/rootdir目录,里面有一个名为ueventd.rc文件,往里面添加一行:  

           /dev/hello 0666 root root    

         定义hello_device_close、hello_set_val和hello_get_val这三个函数:

static int hello_device_close(struct hw_device_t* device) {
	struct hello_device_t* hello_device = (struct hello_device_t*)device;

	if(hello_device) {
		close(hello_device->fd);
		free(hello_device);
	}
	
	return 0;
}

static int hello_set_val(struct hello_device_t* dev, int val) {
	logi("hello stub: set value %d to device.", val);

	write(dev->fd, &val, sizeof(val));

	return 0;
}

static int hello_get_val(struct hello_device_t* dev, int* val) {
	if(!val) {
		loge("hello stub: error val pointer");
		return -efault;
	}

	read(dev->fd, val, sizeof(*val));

	logi("hello stub: get value %d from device", *val);

	return 0;
}

  四. 继续在hello目录下新建android.mk文件:

      local_path := $(call my-dir)
      include $(clear_vars)
      local_module_tags := optional
      local_prelink_module := false
      local_module_path := $(target_out_shared_libraries)/hw
      local_shared_libraries := liblog
      local_src_files := hello.c
      local_module := hello.default
      include $(build_shared_library)

 注意:local_module的定义规则,hello后面跟有default,hello.default能够保证我们的模块总能被硬象抽象层加载到。

       五. 编译:

      user-name@machine-name:~/android$ mmm hardware/libhardware/modules/hello 

      编译成功后,就可以在out/target/product/generic/system/lib/hw目录下看到hello.default.so文件了。

      六. 重新打包android系统镜像system.img:

      user-name@machine-name:~/android$ make snod

      重新打包后,system.img就包含我们定义的硬件抽象层模块hello.default了。

      虽然我们在android系统为我们自己的硬件增加了一个硬件抽象层模块,但是现在java应用程序还不能访问到我们的硬件。我们还必须编写jni方法和在android的application frameworks层增加api接口,才能让上层application访问我们的硬件。在接下来的文章中,我们还将完成这一系统过程,使得我们能够在java应用程序中访问我们自己定制的硬件。

以上就是android硬件抽象层(hal)模块访问linux内核驱动程序的资料整理,后续继续补充,希望能帮助学习android源码的朋友。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网