參考文獻:
http://www.linuxidc.com/Linux/2011-07/38980.htm
http://www.linuxidc.com/Linux/2012-01/50741.htm
建議閱讀本文時先浏覽以上兩篇文章,本文是對上兩篇文章在HAL對上層接口話題的一個總結.
1 什麼是HAL
HAL的全稱是Hardware Abstraction Layer,即硬件抽象層.其架構圖如下:
Android的HAL是為了保護一些硬件提供商的知識產權而提出的,是為了避開linux的GPL束縛。思路是把控制硬件的動作都放到了Android HAL中,而linux driver僅僅完成一些簡單的數據交互作用,甚至把硬件寄存器空間直接映射到user space。而Android是基於Aparch的license,因此硬件廠商可以只提供二進制代碼,所以說Android只是一個開放的平台,並不是一個開源的平台。也許也正是因為Android不遵從GPL,所以Greg Kroah-Hartman才在2.6.33內核將Andorid驅動從linux中刪除。GPL和硬件廠商目前還是有著無法彌合的裂痕。Android想要把這個問題處理好也是不容易的。
總結下來,Android HAL存在的原因主要有:
1. 並不是所有的硬件設備都有標准的linux kernel的接口
2. KERNEL DRIVER涉及到GPL的版權。某些設備制造商並不原因公開硬件驅動,所以才去用HAL方式繞過GPL。
3. 針對某些硬件,Android有一些特殊的需求.
2 與接口相關的幾個結構體
首先來看三個與HAL對上層接口有關的幾個結構體:
- struct hw_module_t; //模塊類型
- struct hw_module_methods_t; //模塊方法
- struct hw_device_t; //設備類型
這幾個數據結構是在Android工作目錄/hardware/libhardware/include/hardware/hardware.h文件中定義.
3 解釋
一般來說,在寫HAL相關代碼時都得包含這個hardware.h頭文件,所以有必要先了解一下這個頭文件中的內容.
- /*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- #ifndef ANDROID_INCLUDE_HARDWARE_HARDWARE_H
- #define ANDROID_INCLUDE_HARDWARE_HARDWARE_H
-
- #include <stdint.h>
- #include <sys/cdefs.h>
-
- #include <cutils/native_handle.h>
- #include <system/graphics.h>
-
- __BEGIN_DECLS
-
- /*
- * Value for the hw_module_t.tag field
- */
-
- #define MAKE_TAG_CONSTANT(A,B,C,D) (((A) << 24) | ((B) << 16) | ((C) << 8) | (D))
-
- #define HARDWARE_MODULE_TAG MAKE_TAG_CONSTANT('H', 'W', 'M', 'T')
- #define HARDWARE_DEVICE_TAG MAKE_TAG_CONSTANT('H', 'W', 'D', 'T')
-
- struct hw_module_t;
- struct hw_module_methods_t;
- struct hw_device_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.
- */
- //每一個硬件模塊都每必須有一個名為HAL_MODULE_INFO_SYM的數據結構變量,它的第一個成員的類型必須為hw_module_t
- typedef struct hw_module_t {
- /** tag must be initialized to HARDWARE_MODULE_TAG */
- uint32_t 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;
-
- /** Name of this module */
- const char *name;
-
- /** Author/owner/implementor of the module */
- const char *author;
-
- /** Modules methods */
- //模塊方法列表,指向hw_module_methods_t*
- 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;
-
- typedef struct hw_module_methods_t { //硬件模塊方法列表的定義,這裡只定義了一個open函數
- /** Open a specific device */
- int (*open)(const struct hw_module_t* module, const char* id, //注意這個open函數明確指出第三個參數的類型為struct hw_device_t**
- struct hw_device_t** device);
- } hw_module_methods_t;
-
- /**
- * Every device data structure must begin with hw_device_t
- * followed by module specific public methods and attributes.
- */
- //每一個設備數據結構的第一個成員函數必須是hw_device_t類型,其次才是各個公共方法和屬性
- typedef struct hw_device_t {
- /** tag must be initialized to HARDWARE_DEVICE_TAG */
- uint32_t tag;
-
- /** version number for hw_device_t */
- uint32_t version;
-
- /** reference to the module this device belongs to */
- struct hw_module_t* module;
-
- /** padding reserved for future use */
- uint32_t reserved[12];
-
- /** Close this device */
- int (*close)(struct hw_device_t* device);
-
- } hw_device_t;
-
- /**
- * Name of the hal_module_info
- */
- #define HAL_MODULE_INFO_SYM HMI
-
- /**
- * Name of the hal_module_info as a string
- */
- #define HAL_MODULE_INFO_SYM_AS_STR "HMI"
-
- /**
- * Get the module info associated with a module by id.
- *
- * @return: 0 == success, <0 == error and *module == NULL
- */
- int hw_get_module(const char *id, const struct hw_module_t **module);
-
- /**
- * Get the module info associated with a module instance by class 'class_id'
- * and instance 'inst'.
- *
- * Some modules types necessitate multiple instances. For example audio supports
- * multiple concurrent interfaces and thus 'audio' is the module class
- * and 'primary' or 'a2dp' are module interfaces. This implies that the files
- * providing these modules would be named audio.primary.<variant>.so and
- * audio.a2dp.<variant>.so
- *
- * @return: 0 == success, <0 == error and *module == NULL
- */
- int hw_get_module_by_class(const char *class_id, const char *inst,
- const struct hw_module_t **module);
-
- __END_DECLS
-
- #endif /* ANDROID_INCLUDE_HARDWARE_HARDWARE_H */
由以上內容可以看出(typedef struct hw_module_t ,typedef struct hw_device_t),如果我們要寫一個自定義設備的驅動的HAL層時,我們得首先自定義兩個數據結構: