参考文献:
http://blog.youkuaiyun.com/luoshengyang/article/details/6573809
http://blog.youkuaiyun.com/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; //设备类型,向上提供接口
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
- */