Android中增加硬件抽象層(HAL)模塊訪問Linux內核驅動程序

在Android中增加硬件抽象層(HAL)模塊訪問Linux內核驅動程序之前,假設我們的驅動程序已經存在可讀可寫的/dev/hello節點。下面就來編寫代碼實現。
一、進入到hardware/libhardware/include/hardware目錄,新建hello.h文件:
cd hardware/libhardware/include/hardware
vim 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    "[email protected]"
 
/*設備打開和關閉接口*/
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
也可以在/device/huawei/vn60/init.target.rc中找個位置更改root root owner
chown system system /dev/hello

接下來定義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能夠保證我們的模塊總能被硬象抽象層加載到。

四、編譯
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了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章