Add Hardware abstraction Layer (HAL) module to Android to access Linux kernel drivers on ubuntu (Lao Luo learning Note 3)

Source: Internet
Author: User

In the Android Hardware Abstraction Layer (HAL) Overview and Learning Plan article, we briefly describe the ways in which Android systems write drivers for hardware. In short, hardware drivers are distributed on the Linux kernel, and on the other hand, in the hardware abstraction layer of user space. Next, writing a Linux kernel driver for Android on Ubuntu cited example shows how to write a driver in the Linux kernel. In this article, we will continue to introduce the implementation of hardware drivers for Android systems on the other hand, that is, how to add hardware modules to the hardware abstraction layer to interact with the kernel driver. In this article, we will also learn how to modify the device file mode with the Linux-like Udev rules when creating device files on Android.

I. Refer to the example kernel driver for the Linux kernel driver written for Android on Ubuntu, as shown in the article. After completing this kernel driver, you can get three files in Android system, namely/dev/hello,/sys/class/hello/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.

Two. Go to the Hardware/libhardware/include/hardware directory and create a new Hello.h file:

[email protected]:~/android$ cd Hardware/libhardware/include/hardware

[Email protected]:~/android/hardware/libhardware/include/hardware$ VI hello.h

the contents of the Hello.h file are as follows:

[CPP]View Plaincopy
  1. #ifndef Android_hello_interface_h
  2. #define Android_hello_interface_h
  3. #include
  4. __begin_decls
  5. /* Define Module id*/
  6. #define HELLO_HARDWARE_MODULE_ID "HELLO"
  7. /* Hardware module structure */
  8. struct hello_module_t {
  9. struct hw_module_t common;
  10. };
  11. /* Hardware Interface structure */
  12. struct hello_device_t {
  13. struct hw_device_t common;
  14. int fd;
  15. Int (*set_val) (struct hello_device_t* dev, int val);
  16. Int (*get_val) (struct hello_device_t* dev, int* val);
  17. };
  18. __end_decls
  19. #endif

In accordance with the requirements of the Android Hardware Abstraction Layer specification, the module ID, module structure and hardware interface structure are defined separately. In the hardware interface structure, FD represents the device file descriptor, which corresponds to the device file "/dev/hello" that we will be working on, and the Set_val and Get_val are the function interfaces provided on the HAL pair.

Three. Go to the Hardware/libhardware/modules directory, create a new Hello directory, and add the hello.c file. HELLO.C more content, we segmented to see.

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

[CPP]View Plaincopy
  1. #define LOG_TAG "Hellostub"
  2. #include
  3. #include
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <cutils/log.h>
  7. #include <cutils/atomic.h>
  8. #define DEVICE_NAME "/dev/hello"
  9. #define MODULE_NAME "Hello"
  10. #define MODULE_AUTHOR "[Email protected]"
  11. /* Device to open and close the interface */
  12. static int Hello_device_open (const struct hw_module_t* module, const char* name, struct HW_  device_t** device);
  13. static int hello_device_close (struct hw_device_t* device);
  14. /* Device Access interface */
  15. static int hello_set_val (struct hello_device_t* dev, int val);
  16. static int hello_get_val (struct hello_device_t* dev, int* val);
  17. /* Module Method Table */
  18. Static struct hw_module_methods_t hello_module_methods = {
  19. Open:hello_device_open
  20. };
  21. /* Module instance variable */
  22. struct hello_module_t Hal_module_info_sym = {
  23. Common: {
  24. Tag:hardware_module_tag,
  25. Version_major:1,
  26. version_minor:0,
  27. ID:HELLO_HARDWARE_MODULE_ID,
  28. Name:module_name,
  29. Author:module_author,
  30. Methods: &hello_module_methods,
  31. }
  32. };

Here, the instance variable name must be Hal_module_info_sym,tag and must be Hardware_module_tag, which is specified by the Android Hardware Abstraction Layer specification.

Define the Hello_device_open function:

[CPP]View Plaincopy
  1. static int Hello_device_open (const struct hw_module_t* module, const char* name, struct HW_ device_t** device) {
  2. struct hello_device_t* Dev;dev = (struct hello_device_t*) malloc (sizeof (struct hello_device_t));
  3. if (!dev) {
  4. LOGE ("Hello stub:failed to Alloc space");
  5. Return-efault;
  6. }
  7. memset (Dev, 0, sizeof (struct hello_device_t));
  8. Dev->common.tag = Hardware_device_tag;
  9. dev->common.version = 0;
  10. Dev->common.module = (hw_module_t*) module;
  11. Dev->common.close = Hello_device_close;
  12. Dev->set_val = Hello_set_val;dev->get_val = Hello_get_val;
  13. if ((dev->fd = open (Device_name, o_rdwr)) = =-1) {
  14. LOGE ("Hello stub:failed to Open/dev/hello--%s.", Strerror (errno)); free (dev);
  15. Return-efault;
  16. }
  17. *device = & (Dev->common);
  18. Logi ("Hello Stub:open/dev/hello successfully.");
  19. return 0;
  20. }

Device_name is defined as "/dev/hello". Since the device files are created in the kernel driver through device_create, and the device files created by Device_create are read and written by default only by the root user, and Hello_device_open is generally called by the upper-level app, These apps typically do not have root privileges, which can cause the device file to fail to open:

Hello stub:failed to Open/dev/hello--Permission denied.The solution is similar to the Udev rules of Linux, open the Android source code project directory, into the System/core/rootdir directory, there is a file named Ueventd.rc, add a line inside: /dev/hello 0666 root root      Define the three functions of Hello_device_close, Hello_set_val, and Hello_get_val: [CPP]View Plaincopy
  1. static int hello_device_close (struct hw_device_t* device) {
  2. struct hello_device_t* hello_device = (struct hello_device_t*) device;
  3. if (hello_device) {
  4. Close (HELLO_DEVICE->FD);
  5. Free (hello_device);
  6. }
  7. return 0;
  8. }
  9. static int hello_set_val (struct hello_device_t* dev, int val) {
  10. Logi ("Hello Stub:set value%d to device.", Val);
  11. Write (DEV->FD, &val, sizeof (Val));
  12. return 0;
  13. }
  14. static int hello_get_val (struct hello_device_t* dev, int* val) {
  15. if (!val) {
  16. LOGE ("Hello stub:error val Pointer");
  17. Return-efault;
  18. }
  19. Read (DEV->FD, Val, sizeof (*val));
  20. Logi ("Hello stub:get value%d from device", *val);
  21. return 0;
  22. }
     Four. Continue to create a new 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 that Local_module's definition rules, followed by default,hello.default, ensure that our modules can always be loaded into the hard-like abstraction layer. Five. Compiling: [email protected]:~/android$ mmm Hardware/libhardware/modules/hello       After the compilation is successful, you can see the hello.default.so file in the OUT/TARGET/PRODUCT/GENERIC/SYSTEM/LIB/HW directory. Six. RePack Android system Image system.img: [email protected]:~/android$ make Snod       After repackaging, SYSTEM.IMG contains our defined Hardware Abstraction Layer module HELLO.DEFAULT. While we have added a hardware Abstraction Layer module to our own hardware on the Android system, Java applications are not yet accessible to our hardware. We also have to write JNI methods and add API interfaces to the Android application frameworks layer to get the upper application access to our hardware. In the next article, we will also complete this system process, enabling us to access our own custom hardware in Java applications.

Add Hardware abstraction Layer (HAL) module to Android to access Linux kernel drivers on ubuntu (Lao Luo learning Note 3)

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.