Android:HAL向上層提供接口

     研究Android的核心庫框架,慢慢的想了解一寫驅動開發,Android怎麼和Linux打交道?下面介紹一個對Android核心框架的HAL(Hardware Abstraction Layer)的理解。Android核心框架如圖:

 

       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有一些特殊的需求.

一、與接口相關的幾個結構體
首先來看三個與HAL對上層接口有關的幾個結構體:
struct hw_module_t;              //模塊類型
struct hw_module_methods_t;      //模塊方法
struct hw_device_t;              //設備類型
這幾個數據結構是在Android工作目錄/hardware/libhardware/include/hardware/hardware.h文件中定義.


二、解釋
  一般來說,在寫HAL相關代碼時都得包含這個hardware.h頭文件,所以有必要先了解一下這個頭文件中的內容.
hardware.h 頭文件內容如下:
#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函數來返回給上層的.




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章