在智能手机时代,每个品牌的手机都有自己的个性特点。正是依靠这种与众不同的个性来吸引用户,营造品牌凝聚力和用户忠城度,典型的代表非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内核驱动程序的编写和编译过程。
一. 参照前面两篇文章Android源码 在Ubuntu上下载,编译和安装和在Android内核源码 在Ubuntu上下载,编译,安装(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文件:
#ifndef _HELLO_ANDROID_H_ #define _HELLO_ANDROID_H_ #include <linux/cdev.h> #include <linux/semaphore.h> #define HELLO_DEVICE_NODE_NAME "hello" #define HELLO_DEVICE_FILE_NAME "hello" #define HELLO_DEVICE_PROC_NAME "hello" #define HELLO_DEVICE_CLASS_NAME "hello" struct hello_android_dev { int val; struct semaphore sem; struct cdev dev; }; #endif
这个头文件定义了一些字符串常量宏,在后面我们要用到。此外,还定义了一个字符设备结构体hello_android_dev,这个就是我们虚拟的硬件设备了,val成员变量就代表设备里面的寄存器,它的类型为int,sem成员变量是一个信号量,是用同步访问寄存器val的,dev成员变量是一个内嵌的字符设备,这个Linux驱动程序自定义字符设备结构体的标准方法。
四.在hello目录中增加hello.c文件,这是驱动程序的实现部分。
驱动程序的功能主要是向上层提供访问设备的寄存器的值,包括读和写。这里,提供了三种访问设备寄存器的方法,一是通过proc文件系统来访问,二是通过传统的设备文件的方法来访问,三是通过devfs文件系统来访问。下面分段描述该驱动程序的实现。
首先是包含必要的头文件和定义三种访问设备的方法:
#include <linux/init.h> #include <linux/module.h> #include <linux/types.h> #include <linux/fs.h> #include <linux/proc_fs.h> #include <linux/device.h> #include <asm/uaccess.h> #include "hello.h" /*主设备和从设备号变量*/ static int hello_major = 0; static int hello_minor = 0; /*设备类别和设备变量*/ static struct class* hello_class = NULL; static struct hello_android_dev* hello_dev = NULL; /*传统的设备文件操作方法*/ static int hello_open(struct inode* inode,struct file* filp); static int hello_release(struct inode* inode,struct file* filp); static ssize_t hello_read(struct file* filp,char __user *buf,size_t count,loff_t* f_pos); static ssize_t hello_write(struct file* filp,const char __user *buf,loff_t* f_pos); /*设备文件操作方法表*/ static struct file_operations hello_fops = { .owner = THIS_MODULE,.open = hello_open,.release = hello_release,.read = hello_read,.write = hello_write,}; /*访问设置属性方法*/ static ssize_t hello_val_show(struct device* dev,struct device_attribute* attr,char* buf); static ssize_t hello_val_store(struct device* dev,const char* buf,size_t count); /*定义设备属性*/ static DEVICE_ATTR(val,S_IRUGO | S_IWUSR,hello_val_show,hello_val_store);
定义传统的设备文件访问方法,主要是定义hello_open、hello_release、hello_read和hello_write这四个打开、释放、读和写设备文件的方法:
/*打开设备方法*/ static int hello_open(struct inode* inode,struct file* filp) { struct hello_android_dev* dev; /*将自定义设备结构体保存在文件指针的私有数据域中,以便访问设备时拿来用*/ dev = container_of(inode->i_cdev,struct hello_android_dev,dev); filp->private_data = dev; return 0; } /*设备文件释放时调用,空实现*/ static int hello_release(struct inode* inode,struct file* filp) { return 0; } /*读取设备的寄存器val的值*/ static ssize_t hello_read(struct file* filp,loff_t* f_pos) { ssize_t err = 0; struct hello_android_dev* dev = filp->private_data; /*同步访问*/ if(down_interruptible(&(dev->sem))) { return -ERESTARTSYS; } if(count < sizeof(dev->val)) { goto out; } /*将寄存器val的值拷贝到用户提供的缓冲区*/ if(copy_to_user(buf,&(dev->val),sizeof(dev->val))) { err = -EFAULT; goto out; } err = sizeof(dev->val); out: up(&(dev->sem)); return err; } /*写设备的寄存器值val*/ static ssize_t hello_write(struct file* filp,loff_t* f_pos) { struct hello_android_dev* dev = filp->private_data; ssize_t err = 0; /*同步访问*/ if(down_interruptible(&(dev->sem))) { return -ERESTARTSYS; } if(count != sizeof(dev->val)) { goto out; } /*将用户提供的缓冲区的值写到设备寄存器去*/ if(copy_from_user(&(dev->val),buf,count)) { err = -EFAULT; goto out; } err = sizeof(dev->val); out: up(&(dev->sem)); return err; }
定义通过devfs文件系统访问方法,这里把设备的寄存器val看成是设备的一个属性,通过读写这个属性来对设备进行访问,主要是实现hello_val_show和hello_val_store两个方法,同时定义了两个内部使用的访问val值的方法__hello_get_val和__hello_set_val:
/*读取寄存器val的值到缓冲区buf中,内部使用*/ static ssize_t __hello_get_val(struct hello_android_dev* dev,char* buf) { int val = 0; /*同步访问*/ if(down_interruptible(&(dev->sem))) { return -ERESTARTSYS; } val = dev->val; up(&(dev->sem)); return snprintf(buf,PAGE_SIZE,"%d\n",val); } /*把缓冲区buf的值写到设备寄存器val中去,内部使用*/ static ssize_t __hello_set_val(struct hello_android_dev* dev,size_t count) { int val = 0; /*将字符串转换成数字*/ val = simple_strtol(buf,NULL,10); /*同步访问*/ if(down_interruptible(&(dev->sem))) { return -ERESTARTSYS; } dev->val = val; up(&(dev->sem)); return count; } /*读取设备属性val*/ static ssize_t hello_val_show(struct device* dev,char* buf) { struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev); return __hello_get_val(hdev,buf); } /*写设备属性val*/ static ssize_t hello_val_store(struct device* dev,size_t count) { struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev); return __hello_set_val(hdev,count); }
定义通过proc文件系统访问方法,主要实现了hello_proc_read和hello_proc_write两个方法,同时定义了在proc文件系统创建和删除文件的方法hello_create_proc和hello_remove_proc:
/*读取设备寄存器val的值,保存在page缓冲区中*/ static ssize_t hello_proc_read(char* page,char** start,off_t off,int count,int* eof,void* data) { if(off > 0) { *eof = 1; return 0; } return __hello_get_val(hello_dev,page); } /*把缓冲区的值buff保存到设备寄存器val中去*/ static ssize_t hello_proc_write(struct file* filp,const char __user *buff,unsigned long len,void* data) { int err = 0; char* page = NULL; if(len > PAGE_SIZE) { printk(KERN_ALERT"The buff is too large: %lu.\n",len); return -EFAULT; } page = (char*)__get_free_page(GFP_KERNEL); if(!page) { printk(KERN_ALERT"Failed to alloc page.\n"); return -ENOMEM; } /*先把用户提供的缓冲区值拷贝到内核缓冲区中去*/ if(copy_from_user(page,buff,len)) { printk(KERN_ALERT"Failed to copy buff from user.\n"); err = -EFAULT; goto out; } err = __hello_set_val(hello_dev,page,len); out: free_page((unsigned long)page); return err; } /*创建/proc/hello文件*/ static void hello_create_proc(void) { struct proc_dir_entry* entry; entry = create_proc_entry(HELLO_DEVICE_PROC_NAME,NULL); if(entry) { entry->owner = THIS_MODULE; entry->read_proc = hello_proc_read; entry->write_proc = hello_proc_write; } } /*删除/proc/hello文件*/ static void hello_remove_proc(void) { remove_proc_entry(HELLO_DEVICE_PROC_NAME,NULL); }