Android HAL layer frame analysis (a)

As an android engage drive or engage in the bottom, I think it is necessary for hal to master, but must reach a certain depth, so I summed up what to write down the entire idea of ​​their own analysis.

Android mainly to see the source code, the source code is obtained according to the idea. (See source code than to see what books are useful book)

What android HAL that? Why have it?

Hardware abstraction layer is between abstract layer structure between the upper layer and android kernel kernel. He is a package of linux-driven, the upper layer to provide a unified interface to the underlying hardware, the upper application does not have to know how to achieve specific work, which shields the underlying implementation details.

It places throughout android architecture as shown below:

The traditional linux operating hardware basically realized the driver in linux kernel space, then why do so now to bother the hardware operation divided into two parts, hal and linux drive it?

Also belonging to the user space hal, linux kernel space belonging to the drive. In fact, not redundant. So why such a thing higher, citing numerous:

1. Google hal setting up the framework, the framework for the upper playing through jni call hal provides a unified api, hardware developers or transplant only need to follow the development of the framework without the realization of the energy bill in the interaction with the upper layer , will focus on hal layer itself can be achieved.

2. From a business perspective, many hardware manufacturers do not want to own some of the core hardware-related things open out, if will own all the hardware drivers into the kernel space driver implementation, you must comply with the GPL, open source is required. Once you have HAL layer, they can achieve something algorithms like HAL in the core layer, and hal layer in the user space, does not belong to the linux kernel, and android source code is as follows appache agreement, this is open source or not open.

 

Hal figure out the meaning of existence, according to analyze the following source hal hal layer in the end is how to achieve a framework and principles, in-depth analysis of what.

Code android hal layer mainly located in / hardware / libhardware Let's go from the top down.

In the layer hal, hal layers of various types of hardware is described in the form of hardware modules hw_module_t structure is described, and each type of hardware modules each have independent hardware, hal is a structure hw_device_t He described the body.

Jni call through the top app hardware, must first get to hw_module_t structure, that is, the hardware module, with this order and then the hardware to operate. Then we take a look at look at these two structures define what it was like.

They are defined in /hardware/libhardware/include/hardware/hardware.h inside.

. A hw_module_t represents a hardware module, which mainly contains the definition of the information, some of the hardware structure of the module:

/**
 * 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.
 */
typedef struct hw_module_t {
    /** tag must be initialized to HARDWARE_MODULE_TAG */
    uint32_t tag;  //tag,根据引文注释可以看到必须被初始化为HARDWARE_MODULE_TAG

    /** major version number for the module */
    uint16_t version_major;//主版本号

    /** minor version number of the module */
    uint16_t version_minor;//次版本号

    /** Identifier of module */
    const char *id;//模块id字符串

    /** Name of this module */
    const char *name;//模块名

    /** Author/owner/implementor of the module */
    const char *author;//作者

    /** Modules methods */
    struct hw_module_methods_t* methods;//硬件模块方法结构体

    /** module's dso */
    void* dso;//打开硬件模块的库时得到的句柄

    /** padding to 128 bytes, reserved for future use */
    uint32_t reserved[32-7];

} hw_module_t;

 Front tag, name that several members of the property will not say, read the comment I believe we all know, take a look at the following hw_module_methods_t, this pointer methods it points to is a structure associated with this method of hardware modules, which can not see I guess there must be some function pointer, but there is only a function pointer. We can look at the definition:

1 typedef struct hw_module_methods_t {
2     /** Open a specific device */
3     int (*open)(const struct hw_module_t* module, const char* id,//打开硬件设备函数指针
4             struct hw_device_t** device);
5 
6 } hw_module_methods_t;

We can see really only a function pointer, open it open function hardware module hardware devices.

Then a member of the void * dso, which is open after the amount of the returned handle device hardware modules related to it, the source function in the rear view hw_get_module when you will understand.

b. Here we look at hw_device_t structure, this structure is mainly used to describe the attribute information module hardware or something. Hardware module may have a plurality of hardware devices.

For example, the sensor module, sensor_module, is a hardware module, but the phone in the corresponding sensor on a good variety, such as acceleration acc_sensor, magnetic sensors M_sensor, etc., they all belong to sensor_module, but they have had their own

hw_device_t structure will be described. hw_device_t definition:

 1 /**
 2  * Every device data structure must begin with hw_device_t
 3  * followed by module specific public methods and attributes.
 4  */
 5 typedef struct hw_device_t {
 6     /** tag must be initialized to HARDWARE_DEVICE_TAG */
 7     uint32_t tag;   //设备tag
 8 
 9     /** version number for hw_device_t */
10     uint32_t version;//版本
11 
12     /** reference to the module this device belongs to */
13     struct hw_module_t* module;//本设备归属的硬件模块
14 
15     /** padding reserved for future use */
16     uint32_t reserved[12];//保留
17 
18     /** Close this device */
19     int (*close)(struct hw_device_t* device);//关闭设备的函数指针
20 
21 } hw_device_t;

Wherein the third member hardware module is directed module structure of this apparatus belongs.

Finally, a function pointer points to close is definitely off the device functions.

 

Well, this, the two main structures hal is finished, the next one we continue, will combine source code, hal layer in the end to see how this works, take a look at the top how to get hardware modules, hardware devices, and in the end It is how to resolve dynamically loaded shared library.

Published 407 original articles · won praise 150 · views 380 000 +

Guess you like

Origin blog.csdn.net/ds1130071727/article/details/104908771