參考文獻:
http://blog.csdn.net/luoshengyang/article/details/6573809
http://blog.csdn.net/hongtao_liu/article/details/6060734
建議閱讀本文時先瀏覽以上兩篇文章,本文是對上兩篇文章在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層時,我們得首先自定義兩個數據結構:
假設我們要做的設備名為XXX:
在頭文件中定義:XXX.h
/*定義模塊ID*/ #define XXX_HARDWARE_MODULE_ID "XXX" /*硬件模塊結構體*/ //見hardware.h中的hw_module_t定義的說明,xxx_module_t的第一個成員必須是hw_module_t類型,其次才是模塊的一此相關信息,當然也可以不定義, //這里就沒有定義模塊相關信息 struct xxx_module_t { struct hw_module_t common; }; /*硬件接口結構體*/ //見hardware.h中的hw_device_t的說明,要求自定義xxx_device_t的第一個成員必須是hw_device_t類型,其次才是其它的一些接口信息. struct xxx_device_t { struct hw_device_t common; //以下成員是HAL對上層提供的接口或一些屬性 int fd; int (*set_val)(struct xxx_device_t* dev, int val); int (*get_val)(struct xxx_device_t* dev, int* val); };注:特別注意xxx_device_t的結構定義,這個才是HAL向上層提供接口函數的數據結構,其成員就是我們想要關心的接口函數.
接下來我們在實現文件XXX.c文件中定義一個xxx_module_t的變量:
/*模塊實例變量*/ struct xxx_module_t HAL_MODULE_INFO_SYM = { //變量名必須為HAL_MODULE_INFO_SYM,這是強制要求的,你要寫Android的HAL就得遵循這個游戲規則, //見hardware.h中的hw_module_t的類型信息說明. common: { tag: HARDWARE_MODULE_TAG, version_major: 1, version_minor: 0, id: XXX_HARDWARE_MODULE_ID, //頭文件中有定義 name: MODULE_NAME, author: MODULE_AUTHOR, methods: &xxx_module_methods, //模塊方法列表,在本地定義 } };
注意到上面有HAL_MODULE_INFO_SYM變量的成員common中包含一個函數列表xxx_module_methods,而這個成員函數列表是在本地自定義的。那么這個成員函數列是不是就是HAL向上層提供函數的地方呢?很失望,不是在這里,前面我們已經說過了,是在xxx_device_t中定義的,這個xxx_module_methods實際上只提供了一個open函數,就相當于只提供了一個模塊初始化函數.其定義如下:
/*模塊方法表*/ static struct hw_module_methods_t xxx_module_methods = { open: xxx_device_open };注意到,上邊的函數列表中只列出了一個xxx_device_open函數,這個函數也是需要在本地實現的一個函數。前面說過,這個函數只相當于模塊初始化函數。
那么HAL又到底是怎么將xxx_device_t中定義的接口提供到上層去的呢?
且看上面這個函數列表中唯一的一個xxx_device_open的定義:
static int xxx_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) { struct xxx_device_t* dev; dev = (struct hello_device_t*)malloc(sizeof(struct xxx_device_t));//動態分配空間 if(!dev) { LOGE("Hello Stub: failed to alloc space"); return -EFAULT; } memset(dev, 0, sizeof(struct xxx_device_t)); //對dev->common的內容賦值, dev->common.tag = HARDWARE_DEVICE_TAG; dev->common.version = 0; dev->common.module = (hw_module_t*)module; dev->common.close = xxx_device_close; //對dev其它成員賦值 dev->set_val = xxx_set_val; dev->get_val = xxx_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; } //輸出&(dev->common),輸出的并不是dev,而是&(dev->common)!(common內不是只包含了一個close接口嗎?) *device = &(dev->common); LOGI("Hello Stub: open /dev/hello successfully."); return 0; }經驗告訴我們,一般在進行模塊初始化的時候,模塊的接口函數也會“注冊”,上面是模塊初始化函數,那么接口注冊在哪?于是我們找到*device =&(dev->common);這行代碼,可問題是,這樣一來,返回給調用者不是&(dev->common)嗎?而這個dev->common僅僅只包含了一個模塊關閉接口!到底怎么回事?為什么不直接返回dev,dev下不是提供所有HAL向上層接口嗎?
在回答上述問題之前,讓我們先看一下這xxx_device_open函數原型,還是在hardware.h頭文件中,找到下面幾行代碼:
typedef struct hw_module_methods_t { /** Open a specific device */ int (*open)(const struct hw_module_t* module, const char* id, struct hw_device_t** device); } hw_module_methods_t;這是方法列表的定義,明確要求了方法列表中有且只一個open方法,即相當于模塊初始化方法,且,這個方法的第三個參數明確指明了類型是struct hw_device_t **,而不是用戶自定義的xxx_device_t,這也就是解譯了在open函數實現內為什么輸出的是&(dev->common)而不是dev了,原來返回的類型在hardware.h中的open函數原型中明確指出只能返回hw_device_t類型.
可是,dev->common不是只包含close接口嗎?做為HAL的上層,它又是怎么"看得到"HAL提供的全部接口的呢?
接下來,讓我們來看看做為HAL上層,它又是怎么使用由HAL返回的dev->common的:
參考: 在Ubuntu為Android硬件抽象層(HAL)模塊編寫JNI方法提供Java訪問硬件服務接口 這篇文章,從中可以看到這么幾行代碼:
/*通過硬件抽象層定義的硬件模塊打開接口打開硬件設備*/ static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) { return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device); }由此可見,返回的&(dev->common)最終會返回給struce hello_device_t **類型的輸出變量device,換句話說,類型為hw_device_t的dev->common在初始化函數open返回后,會強制轉化為xxx_device_t來使用,終于明白了,原來如此!另外,在hardware.h中對xxx_device_t類型有說明,要求它的 第一個成員的類型必須是 hw_device_t,原來是為了HAL上層使用時的強制轉化的目的,如果xxx_device_t的第一個成員類型不是hw_device_t,那么HAL上層使用中強制轉化就沒有意義了,這個時候,就真的“看不到”HAL提供的接口了.
此外,在hardware.h頭文件中,還有明確要求定義xxx_module_t類型時,明確要求 第一個成員變量類型必須為 hw_module_t,這也是為了方便找到其第一個成員變量common,進而找到本地定義的方法列表,從而調用open函數進行模塊初始化.
綜上所述,HAL是通過 struct xxx_device_t 這個結構體向上層提供接口的.
即:接口包含在 struct xxx_device_t 這個結構體內。
而具體執行是通過struct xxx_module_t HAL_MODULE_INFO_SYM這個結構體變量的函數列表成員下的open函數來返回給上層的.
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
