(三) 在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文件:

      USER-NAME@MACHINE-NAME:~/Android$ cd hardware/libhardware/include/hardware
      USER-NAME@MACHINE-NAME:~/Android/hardware/libhardware/include/hardware$ vi hello.h


      hello.h文件的內容如下:

[html] view plaincopy
  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對上提供的函數接口。


      三. 進入到hardware/libhardware/modules目錄,新建hello目錄,並添加hello.c文件。 hello.c的內容較多,我們分段來看。

      首先是包含相關頭文件和定義相關結構:

[html] view plaincopy
  1. #define LOG_TAG "HelloStub"  
  2.   
  3. #include <hardware/hardware.h>  
  4. #include <hardware/hello.h>  
  5. #include <fcntl.h>  
  6. #include <errno.h>  
  7. #include <cutils/log.h>  
  8. #include <cutils/atomic.h>  
  9.   
  10. #define DEVICE_NAME "/dev/hello"  
  11. #define MODULE_NAME "Hello"  
  12. #define MODULE_AUTHOR "[email protected]"  
  13.   
  14. /*設備打開和關閉接口*/  
  15. static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);  
  16. static int hello_device_close(struct hw_device_t* device);  
  17.   
  18. /*設備訪問接口*/  
  19. static int hello_set_val(struct hello_device_t* dev, int val);  
  20. static int hello_get_val(struct hello_device_t* dev, int* val);  
  21.   
  22. /*模塊方法表*/  
  23. static struct hw_module_methods_t hello_module_methods = {  
  24.     open: hello_device_open  
  25. };  
  26.   
  27. /*模塊實例變量*/  
  28. struct hello_module_t HAL_MODULE_INFO_SYM = {  
  29.     common: {  
  30.         tag: HARDWARE_MODULE_TAG,  
  31.         version_major: 1,  
  32.         version_minor: 0,  
  33.         id: HELLO_HARDWARE_MODULE_ID,  
  34.         name: MODULE_NAME,  
  35.         author: MODULE_AUTHOR,  
  36.         methods: &hello_module_methods,  
  37.     }  
  38. };  

   這裏,實例變量名必須爲HAL_MODULE_INFO_SYM,tag也必須爲HARDWARE_MODULE_TAG,這是Android硬件抽象層規範規定的。

      定義hello_device_open函數:

[html] view plaincopy
  1. static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {  
  2.     struct hello_device_t* dev;dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));  
  3.       
  4.     if(!dev) {  
  5.         LOGE("Hello Stub: failed to alloc space");  
  6.         return -EFAULT;  
  7.     }  
  8.   
  9.     memset(dev, 0, sizeof(struct hello_device_t));  
  10.     dev->common.tag = HARDWARE_DEVICE_TAG;  
  11.     dev->common.version = 0;  
  12.     dev->common.module = (hw_module_t*)module;  
  13.     dev->common.close = hello_device_close;  
  14.     dev->set_val = hello_set_val;dev->get_val = hello_get_val;  
  15.   
  16.     if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {  
  17.         LOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);  
  18.         return -EFAULT;  
  19.     }  
  20.   
  21.     *device = &(dev->common);  
  22.     LOGI("Hello Stub: open /dev/hello successfully.");  
  23.   
  24.     return 0;  
  25. }  

DEVICE_NAME定義爲"/dev/hello"。由於設備文件是在內核驅動裏面通過device_create創建的,而device_create創建的設備文件默認只有root用戶可讀寫,而hello_device_open一般是由上層APP來調用的,這些APP一般不具有root權限,這時候就導致打開設備文件失敗:

      
Hello Stub: failed to open /dev/hello -- Permission denied.

      解決辦法是類似於Linux的udev規則,打開Android源代碼工程目錄下,進入到system/core/rootdir目錄,裏面有一個名爲ueventd.rc文件,往裏面添加一行:
      
/dev/hello 0666 root root

      定義hello_device_close、hello_set_val和hello_get_val這三個函數:
[html] view plaincopy
  1. static int hello_device_close(struct hw_device_t* device) {  
  2.     struct hello_device_t* hello_device = (struct hello_device_t*)device;  
  3.   
  4.     if(hello_device) {  
  5.         close(hello_device->fd);  
  6.         free(hello_device);  
  7.     }  
  8.       
  9.     return 0;  
  10. }  
  11.   
  12. static int hello_set_val(struct hello_device_t* dev, int val) {  
  13.     LOGI("Hello Stub: set value %d to device.", val);  
  14.   
  15.     write(dev->fd, &val, sizeof(val));  
  16.   
  17.     return 0;  
  18. }  
  19.   
  20. static int hello_get_val(struct hello_device_t* dev, int* val) {  
  21.     if(!val) {  
  22.         LOGE("Hello Stub: error val pointer");  
  23.         return -EFAULT;  
  24.     }  
  25.   
  26.     read(dev->fd, val, sizeof(*val));  
  27.   
  28.     LOGI("Hello Stub: get value %d from device", *val);  
  29.   
  30.     return 0;  
  31. }  

     四. 繼續在hello目錄下新建Android.mk文件:
      LOCAL_PATH := $(call my-dir)
      include $(CLEAR_VARS)
      LOCAL_MODULE_TAGS := optional
      LOCAL_PRELINK_MODULE := false
      LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
      LOCAL_SHARED_LIBRARIES := liblog
      LOCAL_SRC_FILES := hello.c
      LOCAL_MODULE := hello.default
      include $(BUILD_SHARED_LIBRARY)


      注意,LOCAL_MODULE的定義規則,hello後面跟有default,hello.default能夠保證我們的模塊總能被硬象抽象層加載到。

      五. 編譯:
      USER-NAME@MACHINE-NAME:~/Android$ mmm hardware/libhardware/modules/hello


      編譯成功後,就可以在out/target/product/generic/system/lib/hw目錄下看到hello.default.so文件了。

      六. 把hello.default.so文件通過adb push 到開發板的系統文件的 /system/lib/文件裏面 就可以了
      雖然我們在Android系統爲我們自己的硬件增加了一個硬件抽象層模塊,但是現在Java應用程序還不能訪問到我們的硬件。我們還必須編寫JNI方法和在Android的Application Frameworks層增加API接口,才能讓上層Application訪問我們的硬件。在接下來的文章中,我們還將完成這一系統過程,使得我們能夠在Java應用程序中訪問我們自己定製的硬件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章