Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序

在Android硬件抽象层(HAL)概要介绍和学习计划一文中,我们简要介绍了在Android系统为为硬件编写驱动程序的方法。简单来说,硬件驱动程序一方面分布在Linux内核中,另一方面分布在用户空间的硬件抽象层中。接着,在Ubuntu上为Android系统编写Linux内核驱动程序一文中举例子说明了如何在Linux内核编写驱动程序。在这一篇文章中,我们将继续介绍Android系统硬件驱动程序的另一方面实现,即如何在硬件抽象层中增加硬件模块来和内核驱动程序交互。在这篇文章中,我们还将学习到如何在Android系统创建设备文件时用类似Linux的udev规则修改设备文件模式的方法。一. 参照在Ubuntu上为Android系统编写Linux内核驱动程序一文所示,准备好示例内核驱动序。完成这个内核驱动程序后,便可以在Android系统中得到三个文件,分别是/dev/hello、/sys/class/hello/hello/val和/proc/hello。在本文中,我们将通过设备文件/dev/hello来连接硬件抽象层模块和Linux内核驱动程序模块。二. 进入到在hardware/libhardware/include/hardware目录,新建hello.h文件:linuxidc@www.linuxidc.com:~/Android$ cd hardware/libhardware/include/hardwarelinuxidc@www.linuxidc.com:~/Android/hardware/libhardware/include/hardware$ vi hello.hhello.h文件的内容如下:
  1. #ifndef ANDROID_HELLO_INTERFACE_H   
  2. #define ANDROID_HELLO_INTERFACE_H   
  3. #include <hardware/hardware.h>   
  4.   
  5. __BEGIN_DECLS  
  6.   
  7. /*定义模块ID*/  
  8. #define HELLO_HARDWARE_MODULE_ID "hello"   
  9.   
  10. /*硬件模块结构体*/  
  11. struct hello_module_t {  
  12.     struct hw_module_t common;  
  13. };  
  14.   
  15. /*硬件接口结构体*/  
  16. struct hello_device_t {  
  17.     struct hw_device_t common;  
  18.     int fd;  
  19.     int (*set_val)(struct hello_device_t* dev, int val);  
  20.     int (*get_val)(struct hello_device_t* dev, int* val);  
  21. };  
  22.   
  23. __END_DECLS  
  24.   
  25. #endif  
这里按照Android硬件抽象层规范的要求,分别定义模块ID、模块结构体以及硬件接口结构体。在硬件接口结构体中,fd表示设备文件描述符,对应我们将要处理的设备文件"/dev/hello",set_val和get_val为该HAL对上提供的函数接口。