在Ubuntu上为Android系统编写Linux内核驱动程序.doc

上传人:cn****1 文档编号:544091752 上传时间:2023-09-23 格式:DOC 页数:12 大小:207.50KB
返回 下载 相关 举报
在Ubuntu上为Android系统编写Linux内核驱动程序.doc_第1页
第1页 / 共12页
在Ubuntu上为Android系统编写Linux内核驱动程序.doc_第2页
第2页 / 共12页
在Ubuntu上为Android系统编写Linux内核驱动程序.doc_第3页
第3页 / 共12页
在Ubuntu上为Android系统编写Linux内核驱动程序.doc_第4页
第4页 / 共12页
在Ubuntu上为Android系统编写Linux内核驱动程序.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《在Ubuntu上为Android系统编写Linux内核驱动程序.doc》由会员分享,可在线阅读,更多相关《在Ubuntu上为Android系统编写Linux内核驱动程序.doc(12页珍藏版)》请在金锄头文库上搜索。

1、在智能手机时代,每个品牌的手机都有自己的个性特点。正是依靠这种与众不同的个性来吸引用户,营造品牌凝聚力和用户忠城度,典型的代表非iphone莫属了。据统计,截止2011年5月,AppStore的应用软件数量达381062个,位居第一,而Android Market的应用软件数量达294738,紧随AppStore后面,并有望在8月份越过AppStore。随着Android系统逐步扩大市场占有率,终端设备的多样性亟需更多的移动开发人员的参与。据业内统计,Android研发人才缺口至少30万。目前,对Android人才需求一类是偏向硬件驱动的Android人才需求,一类是偏向软件应用的Androi

2、d人才需求。总的来说,对有志于从事Android硬件驱动的开发工程师来说,现在是一个大展拳脚的机会。那么,就让我们一起来看看如何为Android系统编写内核驱动程序吧。 这里,我们不会为真实的硬件设备编写内核驱动程序。为了方便描述为Android系统编写内核驱动程序的过程,我们使用一个虚拟的硬件设备,这个设备只有一个4字节的寄存器,它可读可写。想起我们第一次学习程序语言时,都喜欢用“Hello, World”作为例子,这里,我们就把这个虚拟的设备命名为“hello”,而这个内核驱动程序也命名为hello驱动程序。其实,Android内核驱动程序和一般Linux内核驱动程序的编写方法是一样的,都

3、是以Linux模块的形式实现的,具体可参考前面Android学习启动篇一文中提到的Linux Device Drivers一书。不过,这里我们还是从Android系统的角度来描述Android内核驱动程序的编写和编译过程。 一. 参照前面两篇文章在Ubuntu上下载、编译和安装Android最新源代码和在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)准备好Android内核驱动程序开发环境。 二. 进入到kernel/common/drivers目录,新建hello目录: USER-NAMEMACHINE-NAME:/Android$ cd kernel

4、/common/drivers USER-NAMEMACHINE-NAME:/Android/kernel/common/drivers$ mkdir hello 三. 在hello目录中增加hello.h文件:view plaincopy to clipboardprint?1. #ifndef_HELLO_ANDROID_H_ 2. #define_HELLO_ANDROID_H_ 3. 4. #include 5. #include 6. 7. #defineHELLO_DEVICE_NODE_NAMEhello 8. #defineHELLO_DEVICE_FILE_NAMEhello

5、 9. #defineHELLO_DEVICE_PROC_NAMEhello 10. #defineHELLO_DEVICE_CLASS_NAMEhello 11. 12. structhello_android_dev13. intval;14. structsemaphoresem;15. structcdevdev;16. ;17. 18. #endif这个头文件定义了一些字符串常量宏,在后面我们要用到。此外,还定义了一个字符设备结构体hello_android_dev,这个就是我们虚拟的硬件设备了,val成员变量就代表设备里面的寄存器,它的类型为int,sem成员变量是一个信号量,是用

6、同步访问寄存器val的,dev成员变量是一个内嵌的字符设备,这个Linux驱动程序自定义字符设备结构体的标准方法。四.在hello目录中增加hello.c文件,这是驱动程序的实现部分。驱动程序的功能主要是向上层提供访问设备的寄存器的值,包括读和写。这里,提供了三种访问设备寄存器的方法,一是通过proc文件系统来访问,二是通过传统的设备文件的方法来访问,三是通过devfs文件系统来访问。下面分段描述该驱动程序的实现。首先是包含必要的头文件和定义三种访问设备的方法:view plaincopy to clipboardprint?1. #include 2. #include 3. #includ

7、e 4. #include 5. #include 6. #include 7. #include 8. 9. #includehello.h 10. 11. /*主设备和从设备号变量*/12. staticinthello_major=0;13. staticinthello_minor=0;14. 15. /*设备类别和设备变量*/16. staticstructclass*hello_class=NULL;17. staticstructhello_android_dev*hello_dev=NULL;18. 19. /*传统的设备文件操作方法*/20. staticinthello_o

8、pen(structinode*inode,structfile*filp);21. staticinthello_release(structinode*inode,structfile*filp);22. staticssize_thello_read(structfile*filp,char_user*buf,size_tcount,loff_t*f_pos);23. staticssize_thello_write(structfile*filp,constchar_user*buf,size_tcount,loff_t*f_pos);24. 25. /*设备文件操作方法表*/26.

9、staticstructfile_operationshello_fops=27. .owner=THIS_MODULE,28. .open=hello_open,29. .release=hello_release,30. .read=hello_read,31. .write=hello_write,32. ;33. 34. /*访问设置属性方法*/35. staticssize_thello_val_show(structdevice*dev,structdevice_attribute*attr,char*buf);36. staticssize_thello_val_store(st

10、ructdevice*dev,structdevice_attribute*attr,constchar*buf,size_tcount);37. 38. /*定义设备属性*/39. staticDEVICE_ATTR(val,S_IRUGO|S_IWUSR,hello_val_show,hello_val_store); 定义传统的设备文件访问方法,主要是定义hello_open、hello_release、hello_read和hello_write这四个打开、释放、读和写设备文件的方法:view plaincopy to clipboardprint?1. /*打开设备方法*/2. st

11、aticinthello_open(structinode*inode,structfile*filp)3. structhello_android_dev*dev;4. 5. /*将自定义设备结构体保存在文件指针的私有数据域中,以便访问设备时拿来用*/6. dev=container_of(inode-i_cdev,structhello_android_dev,dev);7. filp-private_data=dev;8. 9. return0;10. 11. 12. /*设备文件释放时调用,空实现*/13. staticinthello_release(structinode*inod

12、e,structfile*filp)14. return0;15. 16. 17. /*读取设备的寄存器val的值*/18. staticssize_thello_read(structfile*filp,char_user*buf,size_tcount,loff_t*f_pos)19. ssize_terr=0;20. structhello_android_dev*dev=filp-private_data;21. 22. /*同步访问*/23. if(down_interruptible(&(dev-sem)24. return-ERESTARTSYS;25. 26. 27. if(c

13、ountval)28. gotoout;29. 30. 31. /*将寄存器val的值拷贝到用户提供的缓冲区*/32. if(copy_to_user(buf,&(dev-val),sizeof(dev-val)33. err=-EFAULT;34. gotoout;35. 36. 37. err=sizeof(dev-val);38. 39. out:40. up(&(dev-sem);41. returnerr;42. 43. 44. /*写设备的寄存器值val*/45. staticssize_thello_write(structfile*filp,constchar_user*buf,size_tcount,loff_t*f_pos)46. struct

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号