歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中HAL如何向上層提供接口總結

Android中HAL如何向上層提供接口總結

日期:2017/3/1 10:41:26   编辑:Linux編程

參考文獻:

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對上層接口有關的幾個結構體:

  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. #ifndef ANDROID_INCLUDE_HARDWARE_HARDWARE_H
  17. #define ANDROID_INCLUDE_HARDWARE_HARDWARE_H
  18. #include <stdint.h>
  19. #include <sys/cdefs.h>
  20. #include <cutils/native_handle.h>
  21. #include <system/graphics.h>
  22. __BEGIN_DECLS
  23. /*
  24. * Value for the hw_module_t.tag field
  25. */
  26. #define MAKE_TAG_CONSTANT(A,B,C,D) (((A) << 24) | ((B) << 16) | ((C) << 8) | (D))
  27. #define HARDWARE_MODULE_TAG MAKE_TAG_CONSTANT('H', 'W', 'M', 'T')
  28. #define HARDWARE_DEVICE_TAG MAKE_TAG_CONSTANT('H', 'W', 'D', 'T')
  29. struct hw_module_t;
  30. struct hw_module_methods_t;
  31. struct hw_device_t;
  32. /**
  33. * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
  34. * and the fields of this data structure must begin with hw_module_t
  35. * followed by module specific information.
  36. */
  37. //每一個硬件模塊都每必須有一個名為HAL_MODULE_INFO_SYM的數據結構變量,它的第一個成員的類型必須為hw_module_t
  38. typedef struct hw_module_t {
  39. /** tag must be initialized to HARDWARE_MODULE_TAG */
  40. uint32_t tag;
  41. /** major version number for the module */
  42. uint16_t version_major;
  43. /** minor version number of the module */
  44. uint16_t version_minor;
  45. /** Identifier of module */
  46. const char *id;
  47. /** Name of this module */
  48. const char *name;
  49. /** Author/owner/implementor of the module */
  50. const char *author;
  51. /** Modules methods */
  52. //模塊方法列表,指向hw_module_methods_t*
  53. struct hw_module_methods_t* methods;
  54. /** module's dso */
  55. void* dso;
  56. /** padding to 128 bytes, reserved for future use */
  57. uint32_t reserved[32-7];
  58. } hw_module_t;
  59. typedef struct hw_module_methods_t { //硬件模塊方法列表的定義,這裡只定義了一個open函數
  60. /** Open a specific device */
  61. int (*open)(const struct hw_module_t* module, const char* id, //注意這個open函數明確指出第三個參數的類型為struct hw_device_t**
  62. struct hw_device_t** device);
  63. } hw_module_methods_t;
  64. /**
  65. * Every device data structure must begin with hw_device_t
  66. * followed by module specific public methods and attributes.
  67. */
  68. //每一個設備數據結構的第一個成員函數必須是hw_device_t類型,其次才是各個公共方法和屬性
  69. typedef struct hw_device_t {
  70. /** tag must be initialized to HARDWARE_DEVICE_TAG */
  71. uint32_t tag;
  72. /** version number for hw_device_t */
  73. uint32_t version;
  74. /** reference to the module this device belongs to */
  75. struct hw_module_t* module;
  76. /** padding reserved for future use */
  77. uint32_t reserved[12];
  78. /** Close this device */
  79. int (*close)(struct hw_device_t* device);
  80. } hw_device_t;
  81. /**
  82. * Name of the hal_module_info
  83. */
  84. #define HAL_MODULE_INFO_SYM HMI
  85. /**
  86. * Name of the hal_module_info as a string
  87. */
  88. #define HAL_MODULE_INFO_SYM_AS_STR "HMI"
  89. /**
  90. * Get the module info associated with a module by id.
  91. *
  92. * @return: 0 == success, <0 == error and *module == NULL
  93. */
  94. int hw_get_module(const char *id, const struct hw_module_t **module);
  95. /**
  96. * Get the module info associated with a module instance by class 'class_id'
  97. * and instance 'inst'.
  98. *
  99. * Some modules types necessitate multiple instances. For example audio supports
  100. * multiple concurrent interfaces and thus 'audio' is the module class
  101. * and 'primary' or 'a2dp' are module interfaces. This implies that the files
  102. * providing these modules would be named audio.primary.<variant>.so and
  103. * audio.a2dp.<variant>.so
  104. *
  105. * @return: 0 == success, <0 == error and *module == NULL
  106. */
  107. int hw_get_module_by_class(const char *class_id, const char *inst,
  108. const struct hw_module_t **module);
  109. __END_DECLS
  110. #endif /* ANDROID_INCLUDE_HARDWARE_HARDWARE_H */

由以上內容可以看出(typedef struct hw_module_t ,typedef struct hw_device_t),如果我們要寫一個自定義設備的驅動的HAL層時,我們得首先自定義兩個數據結構:

Copyright © Linux教程網 All Rights Reserved