android驱动
来源:互联网 发布:亚马逊 知乎 编辑:程序博客网 时间:2024/05/19 16:21
Android中HAL如何向上层提供接口总结
参考文献:
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对上层接口有关的几个结构体:
1
struct
hw_module_t;
//模块类型
2
struct
hw_module_methods_t;
//模块方法
3
struct
hw_device_t;
//设备类型
3 解释
一般来说,在写HAL相关代码时都得包含这个hardware.h头文件,所以有必要先了解一下这个头文件中的内容.
001
/*
002
* Copyright (C) 2008 The Android Open Source Project
003
*
004
* Licensed under the Apache License, Version 2.0 (the "License");
005
* you may not use this file except in compliance with the License.
006
* You may obtain a copy of the License at
007
*
008
* http://www.apache.org/licenses/LICENSE-2.0
009
*
010
* Unless required by applicable law or agreed to in writing, software
011
* distributed under the License is distributed on an "AS IS" BASIS,
012
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013
* See the License for the specific language governing permissions and
014
* limitations under the License.
015
*/
016
017
#ifndef ANDROID_INCLUDE_HARDWARE_HARDWARE_H
018
#define ANDROID_INCLUDE_HARDWARE_HARDWARE_H
019
020
#include <stdint.h>
021
#include <sys/cdefs.h>
022
023
#include <cutils/native_handle.h>
024
#include <system/graphics.h>
025
026
__BEGIN_DECLS
027
028
/*
029
* Value for the hw_module_t.tag field
030
*/
031
032
#define MAKE_TAG_CONSTANT(A,B,C,D) (((A) << 24) | ((B) << 16) | ((C) << 8) | (D))
033
034
#define HARDWARE_MODULE_TAG MAKE_TAG_CONSTANT('H', 'W', 'M', 'T')
035
#define HARDWARE_DEVICE_TAG MAKE_TAG_CONSTANT('H', 'W', 'D', 'T')
036
037
struct
hw_module_t;
038
struct
hw_module_methods_t;
039
struct
hw_device_t;
040
041
/**
042
* Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
043
* and the fields of this data structure must begin with hw_module_t
044
* followed by module specific information.
045
*/
046
//每一个硬件模块都每必须有一个名为HAL_MODULE_INFO_SYM的数据结构变量,它的第一个成员的类型必须为hw_module_t
047
typedef
struct
hw_module_t {
048
/** tag must be initialized to HARDWARE_MODULE_TAG */
049
uint32_t tag;
050
051
/** major version number for the module */
052
uint16_t version_major;
053
054
/** minor version number of the module */
055
uint16_t version_minor;
056
057
/** Identifier of module */
058
const
char
*id;
059
060
/** Name of this module */
061
const
char
*name;
062
063
/** Author/owner/implementor of the module */
064
const
char
*author;
065
066
/** Modules methods */
067
//模块方法列表,指向hw_module_methods_t*
068
struct
hw_module_methods_t* methods;
069
070
/** module's dso */
071
void
* dso;
072
073
/** padding to 128 bytes, reserved for future use */
074
uint32_t reserved[32-7];
075
076
} hw_module_t;
077
078
typedef
struct
hw_module_methods_t {
//硬件模块方法列表的定义,这里只定义了一个open函数
079
/** Open a specific device */
080
int
(*open)(
const
struct
hw_module_t* module,
const
char
* id,
//注意这个open函数明确指出第三个参数的类型为struct hw_device_t**
081
struct
hw_device_t** device);
082
} hw_module_methods_t;
083
084
/**
085
* Every device data structure must begin with hw_device_t
086
* followed by module specific public methods and attributes.
087
*/
088
//每一个设备数据结构的第一个成员函数必须是hw_device_t类型,其次才是各个公共方法和属性
089
typedef
struct
hw_device_t {
090
/** tag must be initialized to HARDWARE_DEVICE_TAG */
091
uint32_t tag;
092
093
/** version number for hw_device_t */
094
uint32_t version;
095
096
/** reference to the module this device belongs to */
097
struct
hw_module_t* module;
098
099
/** 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 */
假设我们要做的设备名为XXX:
在头文件中定义:XXX.h
01
/*定义模块ID*/
02
#define XXX_HARDWARE_MODULE_ID "XXX"
03
04
/*硬件模块结构体*/
05
//见hardware.h中的hw_module_t定义的说明,xxx_module_t的第一个成员必须是hw_module_t类型,其次才是模块的一此相关信息,当然也可以不定义,
06
//这里就没有定义模块相关信息
07
struct
xxx_module_t {
08
struct
hw_module_t common;
09
};
10
11
/*硬件接口结构体*/
12
//见hardware.h中的hw_device_t的说明,要求自定义xxx_device_t的第一个成员必须是hw_device_t类型,其次才是其它的一些接口信息.
13
struct
xxx_device_t {
14
struct
hw_device_t common;
15
//以下成员是HAL对上层提供的接口或一些属性
16
int
fd;
17
int
(*set_val)(
struct
xxx_device_t* dev,
int
val);
18
int
(*get_val)(
struct
xxx_device_t* dev,
int
* val);
19
};
接下来我们在实现文件XXX.c文件中定义一个xxx_module_t的变量:
01
/*模块实例变量*/
02
struct
xxx_module_t HAL_MODULE_INFO_SYM = {
//变量名必须为HAL_MODULE_INFO_SYM,这是强制要求的,你要写Android的HAL就得遵循这个游戏规则,
03
//见hardware.h中的hw_module_t的类型信息说明.
04
common: {
05
tag: HARDWARE_MODULE_TAG,
06
version_major: 1,
07
version_minor: 0,
08
id: XXX_HARDWARE_MODULE_ID,
//头文件中有定义
09
name: MODULE_NAME,
10
author: MODULE_AUTHOR,
11
methods: &xxx_module_methods,
//模块方法列表,在本地定义
12
}
13
};
1
/*模块方法表*/
2
static
struct
hw_module_methods_t xxx_module_methods = {
3
open: xxx_device_open
4
};
那么HAL又到底是怎么将xxx_device_t中定义的接口提供到上层去的呢?
且看上面这个函数列表中唯一的一个xxx_device_open的定义:
01
static
int
xxx_device_open(
const
struct
hw_module_t* module,
const
char
* name,
struct
hw_device_t** device) {
02
struct
xxx_device_t* dev;
03
dev = (
struct
hello_device_t*)
malloc
(
sizeof
(
struct
xxx_device_t));
//动态分配空间
04
05
if
(!dev) {
06
LOGE(
"Hello Stub: failed to alloc space"
);
07
return
-EFAULT;
08
}
09
10
memset
(dev, 0,
sizeof
(
struct
xxx_device_t));
11
//对dev->common的内容赋值,
12
dev->common.tag = HARDWARE_DEVICE_TAG;
13
dev->common.version = 0;
14
dev->common.module = (hw_module_t*)module;
15
dev->common.close = xxx_device_close;
16
//对dev其它成员赋值
17
dev->set_val = xxx_set_val;
18
dev->get_val = xxx_get_val;
19
20
if
((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
21
LOGE(
"Hello Stub: failed to open /dev/hello -- %s."
,
strerror
(
errno
));
22
free
(dev);
23
return
-EFAULT;
24
}
25
26
//输出&(dev->common),输出的并不是dev,而是&(dev->common)!(common内不是只包含了一个close接口吗?)
27
*device = &(dev->common);
28
LOGI(
"Hello Stub: open /dev/hello successfully."
);
29
30
return
0;
31
}
在回答上述问题之前,让我们先看一下这xxx_device_open函数原型,还是在hardware.h头文件中,找到下面几行代码:
1
typedef
struct
hw_module_methods_t {
2
/** Open a specific device */
3
int
(*open)(
const
struct
hw_module_t* module,
const
char
* id,
4
struct
hw_device_t** device);
5
6
} hw_module_methods_t;
可是,dev->common不是只包含close接口吗?做为HAL的上层,它又是怎么"看得到"HAL提供的全部接口的呢?
接下来,让我们来看看做为HAL上层,它又是怎么使用由HAL返回的dev->common的:
参考: 在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口 这篇文章,从中可以看到这么几行代码:
1
/*通过硬件抽象层定义的硬件模块打开接口打开硬件设备*/
2
static
inline
int
hello_device_open(
const
hw_module_t* module,
struct
hello_device_t** device) {
3
return
module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (
struct
hw_device_t**)device);
4
}
此外,在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函数来返回给上层的.
文章出处:http://blog.csdn.net/flydream0/article/details/7086273
- Android驱动
- Android驱动
- android驱动
- android驱动
- Android驱动
- android 驱动
- Android驱动
- Android BCM4330 FM 驱动驱动
- Android usb 驱动
- Android 键盘驱动
- Android jogball 驱动分析
- Android AC97驱动杂记
- android 背光驱动
- android驱动开发
- ZTE ANDROID RIL驱动
- Android OEM USB 驱动
- Android驱动大全
- Android jogball 驱动分析
- VC++实现IP与ARP信息获取,可以同理实现APR攻击
- Android编译大全
- 如何成为一个出色的网站架构师
- Thrift入门教程
- jQuery的图片/文字无缝滚动
- android驱动
- python函数引用传递测试
- Eclipse快捷键大全
- CS与BS区别
- samba服务器创立及附上自己和别人的实例及一些无法登陆的问题
- 密码管理
- 线程同步处理
- ORACLE内部函数大全以及与SQLSERVER的区别
- 试着理解instance recovery时为什么先前滚(roll forward)再回滚(roll backward)