Add the hardware abstraction layer (HAL) module for Android on Ubuntu to access the Linux kernel driver.

Source: Internet
Author: User

In the overview and learning plan of the hardware abstraction layer (HAL) of Android, we briefly introduce how to write a driver for the hardware in Android. In short, hardware drivers are distributed in the Linux kernel and the hardware abstraction layer of the user space. Next, the article on compiling the Linux kernel driver for Android on Ubuntu illustrates how to compile the driver on the Linux kernel. In this article, we will continue to introduce the implementation of the hardware driver of the Android system, that is, how to add hardware modules in the hardware abstraction layer to interact with the kernel driver. In this article, we will also learn how to modify the file mode of a device using a udev rule similar to Linux when creating a device file in Android.

1. Prepare the sample kernel driver sequence as shown in the article compiling the Linux kernel driver for the Android system on Ubuntu. After completing this kernel driver, you can get three files in the Android system, they are/dev/Hello,/sys/class/Hello/Val, And/proc/hello. In this article, we will connect the hardware abstraction layer module and the Linux kernel driver module through the device file/dev/hello.

2. Go to the hardware/libhardware/include/hardware directory and create a hello. h file:

User-name @ machine-Name :~ /Android $ CD Hardware/libhardware/include/hardware

User-name @ machine-Name :~ /Android/hardware/libhardware/include/hardware $ VI hello. h

The content of the hello. h file is as follows:

# Ifndef android_hello_interface_h # define android_hello_interface_h # include <Hardware/hardware. h >__ begin_decls/* define module ID */# define hello_hardware_module_id "hello"/* hardware module struct */struct hello_module_t {struct hw_module_t common ;}; /* hardware interface struct */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

According to the requirements of the Android hardware abstraction layer specification, the module ID, module structure, and hardware interface structure are defined respectively. In the hardware interface struct, FD indicates the device file descriptor, corresponding to the device file "/dev/Hello" We are going to process. set_val and get_val are the function interfaces provided for the Hal pair.

3. Go to the hardware/libhardware/modules directory, create a hello directory, and add the hello. c file. Hello. C has a lot of content. Let's take a look at it in segments.

The first step is to include the relevant header file and define the relevant structure:

# 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"/* interface for enabling and disabling devices */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);/* device access interface */static int hello_set_val (struct hello_device_t * Dev, int Val); static int hello_get_val (struct hello_device_t * Dev, int * val);/* module method table */static struct hw_module_methods_t hello_module_methods = {open: hello_device_open }; /* module instance variable */struct hello_module_t kernel = {common: {Tag: kernel, version_major: 1, version_minor: 0, ID: kernel, name: module_name, Author: module_author, methods: & hello_module_methods ,}};

Here, the instance variable name must be hal_module_info_sym, And the tag must also be hardware_module_tag, which is defined in the Android hardware abstraction layer.

Define the hello_device_open function:

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 is defined as "/dev/hello ". Because the device file is created in the kernel driver through device_create, and the device file created by device_create can only be read and written by the root user by default, hello_device_open is generally called by the upper-layer app, these apps generally do not have the root permission. In this case, opening the device file fails:

Hello stub: failed to open/dev/Hello -- Permission denied.The solution is similar to the udev rule in Linux. Open the android source code project directory and go to the system/CORE/rootdir Directory, which contains a file named ueventd. RC and add a line to it: /Dev/Hello 0666 Root Define the three functions hello_device_close, hello_set_val, and 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;}

4. Continue to create the Android. mk file in the hello directory:Local_path: = $ (call my-DIR)Include $ (clear_vars)Local_module_tags: = optionalLocal_prelink_module: = falseLocal_module_path: = $ (target_out_shared_libraries)/HWLocal_shared_libraries: = liblogLocal_src_files: = Hello. cLocal_module: = Hello. DefaultInclude $ (build_shared_library)NOTE: For the local_module definition rules, "hello" is followed by "default". "Hello. Default" ensures that our modules are always loaded to the image abstraction layer. 5. Compile:User-name @ machine-Name :~ /Android $ Mmm Hardware/libhardware/modules/HelloAfter compilation, you can see the hello. Default. So file in the out/target/product/generic/system/lib/HW directory. 6. repackage the Android system image system. IMG:User-name @ machine-Name :~ /Android $ make SnodAfter the package is repackaged, system. IMG contains the defined Hardware Abstraction Layer module hello. default. Although we have added a hardware abstraction layer module for our hardware in the Android system, Java applications cannot access our hardware yet. We must also compile the JNI method and add an API interface to the Application Frameworks layer of Android to allow the upper-layer application to access our hardware. In the following article, we will also complete this system process so that we can access our customized hardware in Java applications.Lao Luo's Sina Weibo: http://weibo.com/shengyangluo. welcome to the attention!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.