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

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

        这里,我们不会为真实的硬件设备编写内核驱动程序。为了方便描述为Android系统编写内核驱动程序的过程,我们使用一个虚拟的硬件设备,这个设备只有一个4字节的寄存器,它可读可写。想起我们第一次学习程序语言时,都喜欢用“Hello, World”作为例子,这里,我们就把这个虚拟的设备命名为“hello”,而这个内核驱动程序也命名为hello驱动程序。其实,Android内核驱动程序和一般Linux内核驱动程序的编写方法是一样的,都是以Linux模块的形式实现的,具体可参考前面Android学习启动篇一文中提到的Linux Device Drivers一书。不过,这里我们还是从Android系统的角度来描述Android内核驱动程序的编写和编译过程。
       一. 参照前面两篇文章在Ubuntu上下载、编译和安装Android最新源代码和在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)准备好Android内核驱动程序开发环境。
       二. 进入到kernel/common/drivers目录,新建hello目录:
       USER-NAME@MACHINE-NAME:~/Android$ cd kernel/common/drivers
       USER-NAME@MACHINE-NAME:~/Android/kernel/common/drivers$ mkdir hello
       三. 在hello目录中增加hello.h文件:

[html]  view plain copy
  1.              #ifndef _HELLO_ANDROID_H_     
  2.             #define _HELLO_ANDROID_H_     
  3.             #include <linux/cdev.h>     
  4.             #include <linux/semaphore.h>     
  5.             #define HELLO_DEVICE_NODE_NAME  "hello"     
  6.             #define HELLO_DEVICE_FILE_NAME  "hello"     
  7.             #define HELLO_DEVICE_PROC_NAME  "hello"     
  8.             #define HELLO_DEVICE_CLASS_NAME "hello"     
  9.     
  10.             struct hello_android_dev {    
  11.             int val;    
  12.             struct semaphore sem;    
  13.             struct cdev dev;    
  14. };    
  15. #endif  
[html]  view plain copy
  1.    
[html]  view plain copy
  1. 这个头文件定义了一些字符串常量宏,在后面我们要用到。此外,还定义了一个字符设备结构体hello_android_dev,这个就是我们虚拟的硬件设备了,val成员变量就代表设备里面的寄存器,它的类型为int,sem成员变量是一个信号量,是用同步访问寄存器val的,dev成员变量是一个内嵌的字符设备,这个Linux驱动程序自定义字符设备结构体的标准方法。  
  2. 四.在hello目录中增加hello.c文件,这是驱动程序的实现部分。驱动程序的功能主要是向上层提供访问设备的寄存器的值,包括读和写。这里,提供了三种访问设备寄存器的方法,一是通过proc文件系统来访问,二是通过传统的设备文件的方法来访问,三是通过devfs文件系统来访问。下面分段描述该驱动程序的实现。  
  3. 首先是包含必要的头文件和定义三种访问设备的方法:  
[html]  view plain copy
  1. <pre>  
[html]  view plain copy
  1. #include <linux/init.h>  
  2. #include <linux/module.h>  
  3. #include <linux/types.h>  
  4. #include <linux/fs.h>  
  5. #include <linux/proc_fs.h>  
  6. #include <linux/device.h>  
  7. #include <asm/uaccess.h>  
  8.   
  9. #include "hello.h"  
  10.   
  11. /*主设备和从设备号变量*/  
  12. static int hello_major = 0;  
  13. static int hello_minor = 0;  
  14.   
  15. /*设备类别和设备变量*/  
  16. static struct class* hello_class = NULL;  
  17. static struct hello_android_dev* hello_dev = NULL;  
  18.   
  19. /*传统的设备文件操作方法*/  
  20. static int hello_open(struct inode* inode, struct file* filp);  
  21. static int hello_release(struct inode* inode, struct file* filp);  
  22. static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos);  
  23. static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos);  
  24.   
  25. /*设备文件操作方法表*/  
  26. static struct file_operations hello_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. static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr,  char* buf);  
  36. static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count);  
  37.   
  38. /*定义设备属性*/  
  39. static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store);  


 
是我的实验代码  完全成功的代码

[html]  view plain copy
  1. #if 1 //baiduman 首先是包含必要的头文件和定义三种访问设备的方法  
  2. #include <linux/init.h>  
  3. #include <linux/module.h>  
  4. #include <linux/types.h>  
  5. #include <linux/fs.h>  
  6. #include <linux/proc_fs.h>  
  7. #include <linux/device.h>  
  8. #include <asm/uaccess.h>  
  9. #include "hello.h"  
  10. #if 1//  
  11. #define pr_debug(x...) printk("[hello.c::]:: "x)  
  12. #define ur_debug(x...) printk(x)  
  13. //pr_debug("wang :: %s\n", __func__);  
  14. #else  
  15. #define pr_debug(x...)   do {} while(0)  
  16. #define ur_debug(x...)  do {} while(0)  
  17. #endif  
  18. /*主设备和从设备号变量*/  
  19. static int hello_major = 0;  
  20. static int hello_minor = 0;  
  21.   
  22. /*设备类别和设备变量*/  
  23. static struct class* hello_class = NULL;  
  24. static struct hello_android_dev* hello_dev = NULL;  
  25.   
  26. /*传统的设备文件操作方法*/  
  27. static int hello_open(struct inode* inode, struct file* filp);  
  28. static int hello_release(struct inode* inode, struct file* filp);  
  29. static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos);  
  30. static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos);  
  31.   
  32. /*设备文件操作方法表*/  
  33. static struct file_operations hello_fops = {  
  34.     .owner = THIS_MODULE,  
  35.     .open = hello_open,  
  36.     .release = hello_release,  
  37.     .read = hello_read,  
  38.     .write = hello_write,   
  39. };  
  40. /*访问设置属性方法*/  
  41. static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr,  char* buf);  
  42. static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count);  
  43.   
  44. /*定义设备属性*/  
  45. static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store);  
  46. #endif   
  47. #if 1//baiduman 定义传统的设备文件访问方法,主要是定义hello_open、hello_release、hello_read和hello_write这四个打开、释放、读和写设备文件的方法  
  48.   
  49. /*打开设备方法*/  
  50. static int hello_open(struct inode* inode, struct file* filp) {  
  51.     struct hello_android_dev* dev;          
  52.       
  53.     /*将自定义设备结构体保存在文件指针的私有数据域中,以便访问设备时拿来用*/  
  54.     dev = container_of(inode->i_cdev, struct hello_android_dev, dev);  
  55.     filp->private_data = dev;  
  56.       
  57.     return 0;  
  58. }  
  59.   
  60. /*设备文件释放时调用,空实现*/  
  61. static int hello_release(struct inode* inode, struct file* filp) {  
  62.     return 0;  
  63. }  
  64.   
  65. /*读取设备的寄存器val的值*/  
  66. static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos) {  
  67.     ssize_t err = 0;  
  68.     struct hello_android_dev* dev = filp->private_data;          
  69.   
  70.     /*同步访问*/  
  71.     if(down_interruptible(&(dev->sem))) {  
  72.         return -ERESTARTSYS;  
  73.     }  
  74.   
  75.     if(count < sizeof(dev->val)) {  
  76.         goto out;  
  77.     }          
  78.   
  79.     /*将寄存器val的值拷贝到用户提供的缓冲区*/  
  80.     if(copy_to_user(buf, &(dev->val), sizeof(dev->val))) {  
  81.         err = -EFAULT;  
  82.         goto out;  
  83.     }  
  84.   
  85.     err = sizeof(dev->val);  
  86.   
  87. out:  
  88.     up(&(dev->sem));  
  89.     return err;  
  90. }  
  91.   
  92.   
  93. #endif  
  94. #if 1//baiduman 定义通过devfs文件系统访问方法  
  95.   
  96. /*写设备的寄存器值val*/  
  97. static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos) {  
  98.     struct hello_android_dev* dev = filp->private_data;  
  99.     ssize_t err = 0;          
  100.   
  101.     /*同步访问*/  
  102.     if(down_interruptible(&(dev->sem))) {  
  103.         return -ERESTARTSYS;          
  104.     }          
  105.   
  106.     if(count != sizeof(dev->val)) {  
  107.         goto out;          
  108.     }          
  109.   
  110.     /*将用户提供的缓冲区的值写到设备寄存器去*/  
  111.     if(copy_from_user(&(dev->val), buf, count)) {  
  112.         err = -EFAULT;  
  113.         goto out;  
  114.     }  
  115.     err = sizeof(dev->val);  
  116.   
  117. out:  
  118.     up(&(dev->sem));  
  119.     return err;  
  120. }  
  121.   
  122. /*读取寄存器val的值到缓冲区buf中,内部使用*/  
  123. static ssize_t __hello_get_val(struct hello_android_dev* dev, char* buf)   
  124. {  
  125.       pr_debug("baiduman :: %s\n", __func__);  
  126.     int val = 0;          
  127.     /*同步访问*/  
  128.     if(down_interruptible(&(dev->sem))) {                  
  129.         return -ERESTARTSYS;          
  130.     }          
  131.   
  132.     val = dev->val;          
  133.     up(&(dev->sem));          
  134.   
  135.     return snprintf(buf, PAGE_SIZE, "%d/n", val);  
  136. }  
  137.   
  138. /*把缓冲区buf的值写到设备寄存器val中去,内部使用*/  
  139. static ssize_t __hello_set_val(struct hello_android_dev* dev, const char* buf, size_t count)  
  140. {  
  141.      pr_debug("baiduman :: %s\n", __func__);  
  142.     int val = 0;          
  143.   
  144.     /*将字符串转换成数字*/          
  145.     val = simple_strtol(buf, NULL, 10);          
  146.   
  147.     /*同步访问*/          
  148.     if(down_interruptible(&(dev->sem))) {                  
  149.         return -ERESTARTSYS;          
  150.     }          
  151.   
  152.     dev->val = val;          
  153.     up(&(dev->sem));  
  154.   
  155.     return count;  
  156. }  
  157.   
  158. /*读取设备属性val*/  
  159. static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf)   
  160. {  
  161.     struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);          
  162.     return __hello_get_val(hdev, buf);  
  163. }  
  164.   
  165. /*写设备属性val*/  
  166. static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count)  
  167. {   
  168.     struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);    
  169.     return __hello_set_val(hdev, buf, count);  
  170. }  
  171.   
  172.   
  173. /* proc baiduman*/  
  174. /*读取设备寄存器val的值,保存在page缓冲区中*/  
  175. static ssize_t hello_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data)   
  176. {  
  177.      pr_debug("baiduman :: %s\n", __func__);  
  178.     if(off > 0) {  
  179.         *eof = 1;  
  180.         return 0;  
  181.     }  
  182.   
  183.     return __hello_get_val(hello_dev, page);  
  184. }  
  185.   
  186. /*把缓冲区的值buff保存到设备寄存器val中去*/  
  187. static ssize_t hello_proc_write(struct file* filp, const char __user *buff, unsigned long len, void* data)   
  188. {  
  189.      pr_debug("baiduman :: %s\n", __func__);  
  190.     int err = 0;  
  191.     char* page = NULL;  
  192.   
  193.     if(len > PAGE_SIZE) {  
  194.         printk(KERN_ALERT"The buff is too large: %lu./n", len);  
  195.         return -EFAULT;  
  196.     }  
  197.   
  198.     page = (char*)__get_free_page(GFP_KERNEL);  
  199.     if(!page) {                  
  200.         printk(KERN_ALERT"Failed to alloc page./n");  
  201.         return -ENOMEM;  
  202.     }          
  203.   
  204.     /*先把用户提供的缓冲区值拷贝到内核缓冲区中去*/  
  205.     if(copy_from_user(page, buff, len)) {  
  206.         printk(KERN_ALERT"Failed to copy buff from user./n");                  
  207.         err = -EFAULT;  
  208.         goto out;  
  209.     }  
  210.   
  211.     err = __hello_set_val(hello_dev, page, len);  
  212.   
  213. out:  
  214.     free_page((unsigned long)page);  
  215.     return err;  
  216. }  
  217. #endif  
  218.   
  219. #if 1 //baiduman 定义通过proc文件系统访问方法  
  220. /*创建/proc/hello文件*/  
  221. static void hello_create_proc(void)   
  222. {  
  223.       pr_debug("baiduman :: %s\n", __func__);  
  224.     struct proc_dir_entry* entry;  
  225.       
  226.     entry = create_proc_entry(HELLO_DEVICE_PROC_NAME, 0666, NULL);  
  227.     if(entry) {  
  228.         //entry->owner = THIS_MODULE;  
  229.         entry->read_proc = hello_proc_read;  
  230.         entry->write_proc = hello_proc_write;  
  231.     }  
  232. }  
  233. #if 0  
  234. extern struct proc_dir_entry proc_root;  
  235.         struct proc_dir_entry *root_entry;  
  236.         struct proc_dir_entry *entry;  
  237.         root_entry = proc_mkdir(PROC_NAME, &proc_root);  
  238.         s_proc = create_proc_entry(MODEM_SWITCH_PROC_NAME, 0666, root_entry);  
  239.         if (s_proc != NULL){  
  240.             s_proc->write_proc = modem_switch_writeproc;  
  241.             s_proc->read_proc = modem_switch_readproc;  
  242. }  
  243. #endif  
  244. /*删除/proc/hello文件*/  
  245. static void hello_remove_proc(void) {  
  246.     remove_proc_entry(HELLO_DEVICE_PROC_NAME, NULL);  
  247. }  
  248. #endif  
  249. #if 1//baiduman  定义模块加载和卸载方法,这里只要是执行设备注册和初始化操作  
  250. /*初始化设备*/  
  251. static int  __hello_setup_dev(struct hello_android_dev* dev) {  
  252.     int err;  
  253.     dev_t devno = MKDEV(hello_major, hello_minor);  
  254.   
  255.     memset(dev, 0, sizeof(struct hello_android_dev));  
  256.   
  257.     cdev_init(&(dev->dev), &hello_fops);  
  258.     dev->dev.owner = THIS_MODULE;  
  259.     dev->dev.ops = &hello_fops;          
  260.   
  261.     /*注册字符设备*/  
  262.     err = cdev_add(&(dev->dev),devno, 1);  
  263.     if(err) {  
  264.         return err;  
  265.     }          
  266.   
  267.     /*初始化信号量和寄存器val的值*/  
  268.     init_MUTEX(&(dev->sem));  
  269.     dev->val = 0;  
  270.   
  271.     return 0;  
  272. }  
  273.   
  274. /*模块加载方法*/  
  275. static int __init hello_init(void){   
  276.     int err = -1;  
  277.     dev_t dev = 0;  
  278.     struct device* temp = NULL;  
  279.   
  280.     printk(KERN_ALERT"Initializing hello device./n");          
  281.   
  282.     /*动态分配主设备和从设备号*/  
  283.     err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME);  
  284.     if(err < 0) {  
  285.         printk(KERN_ALERT"Failed to alloc char dev region./n");  
  286.         goto fail;  
  287.     }  
  288.   
  289.     hello_major = MAJOR(dev);  
  290.     hello_minor = MINOR(dev);          
  291.   
  292.     /*分配helo设备结构体变量*/  
  293.     hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL);  
  294.     if(!hello_dev) {  
  295.         err = -ENOMEM;  
  296.         printk(KERN_ALERT"Failed to alloc hello_dev./n");  
  297.         goto unregister;  
  298.     }          
  299.   
  300.     /*初始化设备*/  
  301.     err = __hello_setup_dev(hello_dev);  
  302.     if(err) {  
  303.         printk(KERN_ALERT"Failed to setup dev: %d./n", err);  
  304.         goto cleanup;  
  305.     }          
  306.   
  307.     /*在/sys/class/目录下创建设备类别目录hello*/  
  308.     hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME);  
  309.     if(IS_ERR(hello_class)) {  
  310.         err = PTR_ERR(hello_class);  
  311.         printk(KERN_ALERT"Failed to create hello class./n");  
  312.         goto destroy_cdev;  
  313.     }          
  314.   
  315.     /*在/dev/目录和/sys/class/hello目录下分别创建设备文件hello*/  
  316.     temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);  
  317.     if(IS_ERR(temp)) {  
  318.         err = PTR_ERR(temp);  
  319.         printk(KERN_ALERT"Failed to create hello device.");  
  320.         goto destroy_class;  
  321.     }          
  322.   
  323.     /*在/sys/class/hello/hello目录下创建属性文件val*/  
  324.     err = device_create_file(temp, &dev_attr_val);  
  325.     if(err < 0) {  
  326.         printk(KERN_ALERT"Failed to create attribute val.");                  
  327.         goto destroy_device;  
  328.     }  
  329.   
  330.     dev_set_drvdata(temp, hello_dev);          
  331.   
  332.     /*创建/proc/hello文件*/  
  333.     hello_create_proc();  
  334.   
  335.     printk(KERN_ALERT"Succedded to initialize hello device./n");  
  336.     return 0;  
  337.   
  338. destroy_device:  
  339.     device_destroy(hello_class, dev);  
  340.   
  341. destroy_class:  
  342.     class_destroy(hello_class);  
  343.   
  344. destroy_cdev:  
  345.     cdev_del(&(hello_dev->dev));  
  346.   
  347. cleanup:  
  348.     kfree(hello_dev);  
  349.   
  350. unregister:  
  351.     unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);  
  352.   
  353. fail:  
  354.     return err;  
  355. }  
  356.   
  357. /*模块卸载方法*/  
  358. static void __exit hello_exit(void) {  
  359.     dev_t devno = MKDEV(hello_major, hello_minor);  
  360.   
  361.     printk(KERN_ALERT"Destroy hello device./n");          
  362.   
  363.     /*删除/proc/hello文件*/  
  364.     hello_remove_proc();          
  365.   
  366.     /*销毁设备类别和设备*/  
  367.     if(hello_class) {  
  368.         device_destroy(hello_class, MKDEV(hello_major, hello_minor));  
  369.         class_destroy(hello_class);  
  370.     }          
  371.   
  372.     /*删除字符设备和释放设备内存*/  
  373.     if(hello_dev) {  
  374.         cdev_del(&(hello_dev->dev));  
  375.         kfree(hello_dev);  
  376.     }          
  377.   
  378.     /*释放设备号*/  
  379.     unregister_chrdev_region(devno, 1);  
  380. }  
  381.   
  382. MODULE_LICENSE("GPL");  
  383. MODULE_DESCRIPTION("First Android Driver");  
  384.   
  385. module_init(hello_init);  
  386. module_exit(hello_exit);  
  387.   
  388. #endif  




        定义传统的设备文件访问方法,主要是定义hello_open、hello_release、hello_read和hello_write这四个打开、释放、读和写设备文件的方法:

[html]  view plain copy
  1. /*打开设备方法*/  
  2. static int hello_open(struct inode* inode, struct file* filp) {  
  3.     struct hello_android_dev* dev;          
  4.       
  5.     /*将自定义设备结构体保存在文件指针的私有数据域中,以便访问设备时拿来用*/  
  6.     dev = container_of(inode->i_cdev, struct hello_android_dev, dev);  
  7.     filp->private_data = dev;  
  8.       
  9.     return 0;  
  10. }  
  11.   
  12. /*设备文件释放时调用,空实现*/  
  13. static int hello_release(struct inode* inode, struct file* filp) {  
  14.     return 0;  
  15. }  
  16.   
  17. /*读取设备的寄存器val的值*/  
  18. static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos) {  
  19.     ssize_t err = 0;  
  20.     struct hello_android_dev* dev = filp->private_data;          
  21.   
  22.     /*同步访问*/  
  23.     if(down_interruptible(&(dev->sem))) {  
  24.         return -ERESTARTSYS;  
  25.     }  
  26.   
  27.     if(count < sizeof(dev->val)) {  
  28.         goto out;  
  29.     }          
  30.   
  31.     /*将寄存器val的值拷贝到用户提供的缓冲区*/  
  32.     if(copy_to_user(buf, &(dev->val), sizeof(dev->val))) {  
  33.         err = -EFAULT;  
  34.         goto out;  
  35.     }  
  36.   
  37.     err = sizeof(dev->val);  
  38.   
  39. out:  
  40.     up(&(dev->sem));  
  41.     return err;  
  42. }  
  43.   
  44. /*写设备的寄存器值val*/  
  45. static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos) {  
  46.     struct hello_android_dev* dev = filp->private_data;  
  47.     ssize_t err = 0;          
  48.   
  49.     /*同步访问*/  
  50.     if(down_interruptible(&(dev->sem))) {  
  51.         return -ERESTARTSYS;          
  52.     }          
  53.   
  54.     if(count != sizeof(dev->val)) {  
  55.         goto out;          
  56.     }          
  57.   
  58.     /*将用户提供的缓冲区的值写到设备寄存器去*/  
  59.     if(copy_from_user(&(dev->val), buf, count)) {  
  60.         err = -EFAULT;  
  61.         goto out;  
  62.     }  
  63.   
  64.     err = sizeof(dev->val);  
  65.   
  66. out:  
  67.     up(&(dev->sem));  
  68.     return err;  
  69. }  




        定义通过devfs文件系统访问方法,这里把设备的寄存器val看成是设备的一个属性,通过读写这个属性来对设备进行访问,主要是实现hello_val_show和hello_val_store两个方法,同时定义了两个内部使用的访问val值的方法__hello_get_val和__hello_set_val:

[html]  view plain copy
  1. /*读取寄存器val的值到缓冲区buf中,内部使用*/  
  2. static ssize_t __hello_get_val(struct hello_android_dev* dev, char* buf) {  
  3.     int val = 0;          
  4.   
  5.     /*同步访问*/  
  6.     if(down_interruptible(&(dev->sem))) {                  
  7.         return -ERESTARTSYS;          
  8.     }          
  9.   
  10.     val = dev->val;          
  11.     up(&(dev->sem));          
  12.   
  13.     return snprintf(buf, PAGE_SIZE, "%d/n", val);  
  14. }  
  15.   
  16. /*把缓冲区buf的值写到设备寄存器val中去,内部使用*/  
  17. static ssize_t __hello_set_val(struct hello_android_dev* dev, const char* buf, size_t count) {  
  18.     int val = 0;          
  19.   
  20.     /*将字符串转换成数字*/          
  21.     val = simple_strtol(buf, NULL, 10);          
  22.   
  23.     /*同步访问*/          
  24.     if(down_interruptible(&(dev->sem))) {                  
  25.         return -ERESTARTSYS;          
  26.     }          
  27.   
  28.     dev->val = val;          
  29.     up(&(dev->sem));  
  30.   
  31.     return count;  
  32. }  
  33.   
  34. /*读取设备属性val*/  
  35. static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf) {  
  36.     struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);          
  37.   
  38.     return __hello_get_val(hdev, buf);  
  39. }  
  40.   
  41. /*写设备属性val*/  
  42. static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) {   
  43.     struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);    
  44.       
  45.     return __hello_set_val(hdev, buf, count);  
  46. }  



        定义通过proc文件系统访问方法,主要实现了hello_proc_read和hello_proc_write两个方法,同时定义了在proc文件系统创建和删除文件的方法hello_create_proc和hello_remove_proc:

[html]  view plain copy
  1. /*读取设备寄存器val的值,保存在page缓冲区中*/  
  2. static ssize_t hello_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data) {  
  3.     if(off > 0) {  
  4.         *eof = 1;  
  5.         return 0;  
  6.     }  
  7.   
  8.     return __hello_get_val(hello_dev, page);  
  9. }  
  10.   
  11. /*把缓冲区的值buff保存到设备寄存器val中去*/  
  12. static ssize_t hello_proc_write(struct file* filp, const char __user *buff, unsigned long len, void* data) {  
  13.     int err = 0;  
  14.     char* page = NULL;  
  15.   
  16.     if(len > PAGE_SIZE) {  
  17.         printk(KERN_ALERT"The buff is too large: %lu./n", len);  
  18.         return -EFAULT;  
  19.     }  
  20.   
  21.     page = (char*)__get_free_page(GFP_KERNEL);  
  22.     if(!page) {                  
  23.         printk(KERN_ALERT"Failed to alloc page./n");  
  24.         return -ENOMEM;  
  25.     }          
  26.   
  27.     /*先把用户提供的缓冲区值拷贝到内核缓冲区中去*/  
  28.     if(copy_from_user(page, buff, len)) {  
  29.         printk(KERN_ALERT"Failed to copy buff from user./n");                  
  30.         err = -EFAULT;  
  31.         goto out;  
  32.     }  
  33.   
  34.     err = __hello_set_val(hello_dev, page, len);  
  35.   
  36. out:  
  37.     free_page((unsigned long)page);  
  38.     return err;  
  39. }  
  40.   
  41. /*创建/proc/hello文件*/  
  42. static void hello_create_proc(void) {  
  43.     struct proc_dir_entry* entry;  
  44.       
  45.     entry = create_proc_entry(HELLO_DEVICE_PROC_NAME, 0, NULL);  
  46.     if(entry) {  
  47.         entry->owner = THIS_MODULE;  
  48.         entry->read_proc = hello_proc_read;  
  49.         entry->write_proc = hello_proc_write;  
  50.     }  
  51. }  
  52.   
  53. /*删除/proc/hello文件*/  
  54. static void hello_remove_proc(void) {  
  55.     remove_proc_entry(HELLO_DEVICE_PROC_NAME, NULL);  
  56. }  




最后,定义模块加载和卸载方法,这里只要是执行设备注册和初始化操作:

[html]  view plain copy
  1. /*初始化设备*/  
  2. static int  __hello_setup_dev(struct hello_android_dev* dev) {  
  3.     int err;  
  4.     dev_t devno = MKDEV(hello_major, hello_minor);  
  5.   
  6.     memset(dev, 0, sizeof(struct hello_android_dev));  
  7.   
  8.     cdev_init(&(dev->dev), &hello_fops);  
  9.     dev->dev.owner = THIS_MODULE;  
  10.     dev->dev.ops = &hello_fops;          
  11.   
  12.     /*注册字符设备*/  
  13.     err = cdev_add(&(dev->dev),devno, 1);  
  14.     if(err) {  
  15.         return err;  
  16.     }          
  17.   
  18.     /*初始化信号量和寄存器val的值*/  
  19.     init_MUTEX(&(dev->sem));  
  20.     dev->val = 0;  
  21.   
  22.     return 0;  
  23. }  
  24.   
  25. /*模块加载方法*/  
  26. static int __init hello_init(void){   
  27.     int err = -1;  
  28.     dev_t dev = 0;  
  29.     struct device* temp = NULL;  
  30.   
  31.     printk(KERN_ALERT"Initializing hello device./n");          
  32.   
  33.     /*动态分配主设备和从设备号*/  
  34.     err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME);  
  35.     if(err < 0) {  
  36.         printk(KERN_ALERT"Failed to alloc char dev region./n");  
  37.         goto fail;  
  38.     }  
  39.   
  40.     hello_major = MAJOR(dev);  
  41.     hello_minor = MINOR(dev);          
  42.   
  43.     /*分配helo设备结构体变量*/  
  44.     hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL);  
  45.     if(!hello_dev) {  
  46.         err = -ENOMEM;  
  47.         printk(KERN_ALERT"Failed to alloc hello_dev./n");  
  48.         goto unregister;  
  49.     }          
  50.   
  51.     /*初始化设备*/  
  52.     err = __hello_setup_dev(hello_dev);  
  53.     if(err) {  
  54.         printk(KERN_ALERT"Failed to setup dev: %d./n", err);  
  55.         goto cleanup;  
  56.     }          
  57.   
  58.     /*在/sys/class/目录下创建设备类别目录hello*/  
  59.     hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME);  
  60.     if(IS_ERR(hello_class)) {  
  61.         err = PTR_ERR(hello_class);  
  62.         printk(KERN_ALERT"Failed to create hello class./n");  
  63.         goto destroy_cdev;  
  64.     }          
  65.   
  66.     /*在/dev/目录和/sys/class/hello目录下分别创建设备文件hello*/  
  67.     temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);  
  68.     if(IS_ERR(temp)) {  
  69.         err = PTR_ERR(temp);  
  70.         printk(KERN_ALERT"Failed to create hello device.");  
  71.         goto destroy_class;  
  72.     }          
  73.   
  74.     /*在/sys/class/hello/hello目录下创建属性文件val*/  
  75.     err = device_create_file(temp, &dev_attr_val);  
  76.     if(err < 0) {  
  77.         printk(KERN_ALERT"Failed to create attribute val.");                  
  78.         goto destroy_device;  
  79.     }  
  80.   
  81.     dev_set_drvdata(temp, hello_dev);          
  82.   
  83.     /*创建/proc/hello文件*/  
  84.     hello_create_proc();  
  85.   
  86.     printk(KERN_ALERT"Succedded to initialize hello device./n");  
  87.     return 0;  
  88.   
  89. destroy_device:  
  90.     device_destroy(hello_class, dev);  
  91.   
  92. destroy_class:  
  93.     class_destroy(hello_class);  
  94.   
  95. destroy_cdev:  
  96.     cdev_del(&(hello_dev->dev));  
  97.   
  98. cleanup:  
  99.     kfree(hello_dev);  
  100.   
  101. unregister:  
  102.     unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);  
  103.   
  104. fail:  
  105.     return err;  
  106. }  
  107.   
  108. /*模块卸载方法*/  
  109. static void __exit hello_exit(void) {  
  110.     dev_t devno = MKDEV(hello_major, hello_minor);  
  111.   
  112.     printk(KERN_ALERT"Destroy hello device./n");          
  113.   
  114.     /*删除/proc/hello文件*/  
  115.     hello_remove_proc();          
  116.   
  117.     /*销毁设备类别和设备*/  
  118.     if(hello_class) {  
  119.         device_destroy(hello_class, MKDEV(hello_major, hello_minor));  
  120.         class_destroy(hello_class);  
  121.     }          
  122.   
  123.     /*删除字符设备和释放设备内存*/  
  124.     if(hello_dev) {  
  125.         cdev_del(&(hello_dev->dev));  
  126.         kfree(hello_dev);  
  127.     }          
  128.   
  129.     /*释放设备号*/  
  130.     unregister_chrdev_region(devno, 1);  
  131. }  
  132.   
  133. MODULE_LICENSE("GPL");  
  134. MODULE_DESCRIPTION("First Android Driver");  
  135.   
  136. module_init(hello_init);  
  137. module_exit(hello_exit);  



五.在hello目录中新增Kconfig和Makefile两个文件,其中Kconfig是在编译前执行配置命令make menuconfig时用到的,而Makefile是执行编译命令make是用到的:
Kconfig文件的内容
       config HELLO
           tristate "First Android Driver"
           default n
           help
           This is the first android driver.
      Makefile文件的内容
      obj-$(CONFIG_HELLO) += hello.o
      在Kconfig文件中,tristate表示编译选项HELLO支持在编译内核时,hello模块支持以模块、内建和不编译三种编译方法,默认是不编译,因此,在编译内核前,我们还需要执行make menuconfig命令来配置编译选项,使得hello可以以模块或者内建的方法进行编译。
      在Makefile文件中,根据选项HELLO的值,执行不同的编译方法。
      六. 修改arch/arm/Kconfig和drivers/kconfig两个文件,在menu "Device Drivers"和endmenu之间添加一行:
      source "drivers/hello/Kconfig"
        这样,执行make menuconfig时,就可以配置hello模块的编译选项了。. 
        七. 修改drivers/Makefile文件,添加一行:
        obj-$(CONFIG_HELLO) += hello/
        八. 配置编译选项:
        USER-NAME@MACHINE-NAME:~/Android/kernel/common$ make menuconfig
        找到"Device Drivers" => "First Android Drivers"选项,设置为y。
        注意,如果内核不支持动态加载模块,这里不能选择m,虽然我们在Kconfig文件中配置了HELLO选项为tristate。要支持动态加载模块选项,必须要在配置菜单中选择Enable loadable module support选项;在支持动态卸载模块选项,必须要在Enable loadable module support菜单项中,选择Module unloading选项。
        九. 编译:
        USER-NAME@MACHINE-NAME:~/Android/kernel/common$ make
        编译成功后,就可以在hello目录下看到hello.o文件了,这时候编译出来的zImage已经包含了hello驱动。
        十. 参照在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)一文所示,运行新编译的内核文件,验证hello驱动程序是否已经正常安装:
        USER-NAME@MACHINE-NAME:~/Android$ emulator -kernel ./kernel/common/arch/arm/boot/zImage &
        USER-NAME@MACHINE-NAME:~/Android$ adb shell
        进入到dev目录,可以看到hello设备文件:
        root@android:/ # cd dev
        root@android:/dev # ls
        进入到proc目录,可以看到hello文件:
        root@android:/ # cd proc
        root@android:/proc # ls
        访问hello文件的值:
        root@android:/proc # cat hello
        0
        root@android:/proc # echo '5' > hello
        root@android:/proc # cat hello
        5
        进入到sys/class目录,可以看到hello目录:
        root@android:/ # cd sys/class
        root@android:/sys/class # ls
        进入到hello目录,可以看到hello目录:
        root@android:/sys/class # cd hello
        root@android:/sys/class/hello # ls
        进入到下一层hello目录,可以看到val文件:
        root@android:/sys/class/hello # cd hello
        root@android:/sys/class/hello/hello # ls
        访问属性文件val的值:
        root@android:/sys/class/hello/hello # cat val
        5
        root@android:/sys/class/hello/hello # echo '0'  > val
        root@android:/sys/class/hello/hello # cat val
        0
        至此,我们的hello内核驱动程序就完成了,并且验证一切正常。这里我们采用的是系统提供的方法和驱动程序进行交互,也就是通过proc文件系统和devfs文件系统的方法,下一篇文章中,我们将通过自己编译的C语言程序来访问/dev/hello文件来和hello驱动程序交互,敬请期待。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值