Android中HAL如何向上层提供接口总结


参考文献:

建议阅读本文时先浏览以上两篇文章,本文是对上两篇文章在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对上层接口有关的几个结构体:

  1. struct hw_module_t;              //模块类型  
  2. struct hw_module_methods_t;      //模块方法  
  3. struct hw_device_t;              //设备类型  

这几个数据结构是在Android工作目录/hardware/libhardware/include/hardware/hardware.h文件中定义.

3 解释

一般来说,在写HAL相关代码时都得包含这个hardware.h头文件,所以有必要先了解一下这个头文件中的内容.

  1. /* 
  2.  * Copyright (C) 2008 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. #ifndef ANDROID_INCLUDE_HARDWARE_HARDWARE_H   
  18. #define ANDROID_INCLUDE_HARDWARE_HARDWARE_H   
  19.   
  20. #include <stdint.h>   
  21. #include <sys/cdefs.h>   
  22.   
  23. #include <cutils/native_handle.h>   
  24. #include <system/graphics.h>   
  25.   
  26. __BEGIN_DECLS  
  27.   
  28. /* 
  29.  * Value for the hw_module_t.tag field 
  30.  */  
  31.   
  32. #define MAKE_TAG_CONSTANT(A,B,C,D) (((A) << 24) | ((B) << 16) | ((C) << 8) | (D))   
  33.   
  34. #define HARDWARE_MODULE_TAG MAKE_TAG_CONSTANT('H', 'W', 'M', 'T')   
  35. #define HARDWARE_DEVICE_TAG MAKE_TAG_CONSTANT('H', 'W', 'D', 'T')   
  36.   
  37. struct hw_module_t;  
  38. struct hw_module_methods_t;  
  39. struct hw_device_t;  
  40.   
  41. /** 
  42.  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM 
  43.  * and the fields of this data structure must begin with hw_module_t 
  44.  * followed by module specific information. 
  45.  */  
  46. //每一个硬件模块都每必须有一个名为HAL_MODULE_INFO_SYM的数据结构变量,它的第一个成员的类型必须为hw_module_t   
  47. typedef struct hw_module_t {  
  48.     /** tag must be initialized to HARDWARE_MODULE_TAG */  
  49.     uint32_t tag;  
  50.   
  51.     /** major version number for the module */  
  52.     uint16_t version_major;  
  53.   
  54.     /** minor version number of the module */  
  55.     uint16_t version_minor;  
  56.   
  57.     /** Identifier of module */  
  58.     const char *id;  
  59.   
  60.     /** Name of this module */  
  61.     const char *name;  
  62.   
  63.     /** Author/owner/implementor of the module */  
  64.     const char *author;  
  65.   
  66.     /** Modules methods */  
  67.     //模块方法列表,指向hw_module_methods_t*   
  68.     struct hw_module_methods_t* methods;  
  69.   
  70.     /** module's dso */  
  71.     void* dso;  
  72.   
  73.     /** padding to 128 bytes, reserved for future use */  
  74.     uint32_t reserved[32-7];  
  75.   
  76. } hw_module_t;  
  77.   
  78. typedef struct hw_module_methods_t {                 //硬件模块方法列表的定义,这里只定义了一个open函数   
  79.     /** Open a specific device */  
  80.     int (*open)(const struct hw_module_t* module, const char* id, //注意这个open函数明确指出第三个参数的类型为struct hw_device_t**   
  81.             struct hw_device_t** device);  
  82. } hw_module_methods_t;  
  83.   
  84. /** 
  85.  * Every device data structure must begin with hw_device_t 
  86.  * followed by module specific public methods and attributes. 
  87.  */  
  88. //每一个设备数据结构的第一个成员函数必须是hw_device_t类型,其次才是各个公共方法和属性   
  89. typedef struct hw_device_t {  
  90.     /** tag must be initialized to HARDWARE_DEVICE_TAG */  
  91.     uint32_t tag;  
  92.   
  93.     /** version number for hw_device_t */  
  94.     uint32_t version;  
  95.   
  96.     /** reference to the module this device belongs to */  
  97.     struct hw_module_t* module;  
  98.   
  99.     /** padding reserved for future use */  
  100.     uint32_t reserved[12];  
  101.   
  102.     /** Close this device */  
  103.     int (*close)(struct hw_device_t* device);  
  104.   
  105. } hw_device_t;  
  106.   
  107. /** 
  108.  * Name of the hal_module_info 
  109.  */  
  110. #define HAL_MODULE_INFO_SYM         HMI   
  111.   
  112. /** 
  113.  * Name of the hal_module_info as a string 
  114.  */  
  115. #define HAL_MODULE_INFO_SYM_AS_STR  "HMI"   
  116.   
  117. /** 
  118.  * Get the module info associated with a module by id. 
  119.  * 
  120.  * @return: 0 == success, <0 == error and *module == NULL 
  121.  */  
  122. int hw_get_module(const char *id, const struct hw_module_t **module);  
  123.   
  124. /** 
  125.  * Get the module info associated with a module instance by class 'class_id' 
  126.  * and instance 'inst'. 
  127.  * 
  128.  * Some modules types necessitate multiple instances. For example audio supports 
  129.  * multiple concurrent interfaces and thus 'audio' is the module class 
  130.  * and 'primary' or 'a2dp' are module interfaces. This implies that the files 
  131.  * providing these modules would be named audio.primary.<variant>.so and 
  132.  * audio.a2dp.<variant>.so 
  133.  * 
  134.  * @return: 0 == success, <0 == error and *module == NULL 
  135.  */  
  136. int hw_get_module_by_class(const char *class_id, const char *inst,  
  137.                            const struct hw_module_t **module);  
  138.   
  139. __END_DECLS  
  140.   
  141. #endif  /* ANDROID_INCLUDE_HARDWARE_HARDWARE_H */  

由以上内容可以看出(typedef struct hw_module_t ,typedef struct hw_device_t),如果我们要写一个自定义设备的驱动的HAL层时,我们得首先自定义两个数据结构:

  • 1
  • 2
  • 下一页

相关内容