Add the hardware abstraction layer (HAL) module on Ubuntu to access the Linux Kernel Driver (Lao Luo study note 3) and ubuntuandroid

Source: Internet
Author: User

Add the hardware abstraction layer (HAL) module on Ubuntu to access the Linux Kernel Driver (Lao Luo study note 3) and ubuntuandroid

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. To put it simply,Hardware drivers are distributed in the Linux kernel and in the hardware abstraction layer of the user space.. Next, compile the Linux kernel driver for the Android system on Ubuntu as an example.

In 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

Linux Kernel Driver Module.

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 /* -- Set_val what type? What is the function? And how to reuse the struct in the future ?? --*/Int (* get_val) (struct hello_device_t * dev, int * val);}; _ END_DECLS # endif

/*--

Hw_module_t:

/*** Every hardware module must have a data structure named HAL_MODULE_INFO_SYM * and the fields of this data structure must begin with hw_module_t * followed by module specific information. * /// each hardware module must have a Data Structure Variable named HAL_MODULE_INFO_SYM, the type of its first member must be hw_module_t typedef struct hw_module_t {/** tag must be initialized to HARDWARE_MODULE_TAG */uint32_t tag; // initialize the hardware module tag/** major version number for the module */uint16_t version_major; // master module version/** minor version number of the module */uint16_t version_minor; // sub-module version/** Identifier of module */const char * id; // module Identifier/** Name of this module */const char * name; // module name/** Author/owner/implementor of the module */const char * author; // compile the Author/** Modules methods * // module method list, point to hw_module_methods_t * struct hw_module_methods_t * methods; // method of the hardware module/** module's dso */void * dso; // DSO: Device Software Optimization, device software optimization DSO/** padding to 128 bytes, reserved for future use */uint32_t reserved [32-7]; // fill in to 128 bytes, reserved for future use} hw_module_t;

 

--*/

According to the requirements of the Android hardware abstraction layer specification, define the ① module ID, ② module structure, and ③ hardware interface structure 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"/* -- HelloStub: what is the role? A:

HAL stub's framework is relatively simple. It has three structs, two constants, and one function, or 321 architecture. Its definition is as follows:

@ Hardware/libhardware/include/hardware. h

@ Hardware/libhardware/hardware. c

-- */# Include 

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 = (struct hello_device_t *) malloc (sizeof (struct hello_device_t);/* -- converts the starting address of an allocated domain to the struct hello_device_t * type. If this function fails to be executed successfully (for example, the memory is insufficient ), returns a NULL pointer (NULL, '0') -- */if (! Dev) {LOGE ("Hello Stub: failed to alloc space"); return-EFAULT;} memset (dev, 0, sizeof (struct hello_device_t); // clear the memory of this segment, then the memory segment is assigned 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 ){// When-1 is returned, the module fails to be opened.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. "); // does LOGI print information? Where is the definition? Return 0 ;}

/*--

Shmctl (shared memory management)
Header files # Include <sys/types. h> # include <sys/shm. h>
Function Description Controls the shared memory.
Function prototype Int shmctl (int shmid, int cmd, struct shmid_ds * buf)
Function input value Shmid Shared Memory identifier
Cmd IPC_STAT: Get the shared memory status. Copy the shmid_ds structure of the shared memory to the buf.
IPC_SET: changes the status of the shared memory and copies uid, gid, and mode in the shmid_ds structure referred to by buf to the shmid_ds structure of the shared memory.
IPC_RMID: deletes the shared memory.
Buf Shared memory management structure. For details, see shared memory kernel structure definition.
Function return value Success: 0
Error:-1, cause of error saved in error
Error Code EACCESS: The cmd parameter is IPC_STAT, so you do not have the permission to read the shared memory.EFAULT: The parameter buf points to an invalid memory address.EIDRM: the shared memory with the identifier shmid has been deleted. Val VAL: Invalid parameter cmd or shmidEPERM: The parameter cmd is IPC_SET or IPC_RMID, but there is not enough permission to execute

--*/

DEVICE_NAME is defined as "/dev/hello ". Because the device files are created in the kernel driver through device_createBy default, only root users can read and write device files created by device_create.Hello_device_open is generally called by upper-layer apps. These apps 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: = 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)
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/hello
After 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 snod
After 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
We have to be able to access our customized hardware in Java applications. I am using the tiny4412 Development Board provided by the friendly arm.
During Compilation:

1

Make: Enter the directory '/opt/FriendlyARM/tiny4412/android-4.1.2'


Make: *** you can create "out/target/product/generic/obj/SHARED_LIBRARIES/audio. usb. default_intermediates/import_includes "indicates the target" out/target/product/generic/obj/SHARED_LIBRARIES/libc_intermediates/export_includes ". Stop.


Make: Leave the directory "/opt/FriendlyARM/tiny4412/android-4.1.2"

Remember:. setenv

 

2. modify local programs

Http://blog.csdn.net/oldmtn/article/details/9213869


Original article: http://blog.csdn.net/luoshengyang/article/details/6573809

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.