在Ubuntu上為Android系統編寫Linux核心驅動程式
在智慧手機時代,每個品牌的手機都有自己的個性特點。正是依靠這種與眾不同的個性來吸引使用者,營造品牌凝聚力和使用者忠城度,典型的代表非iphone莫屬了。據統計,截止2011年5月,AppStore的應用軟體數量達381062個,位居第一,而Android Market的應用軟體數量達294738,緊隨AppStore後面,並有望在8月份越過AppStore。隨著Android系統逐步擴大市場佔有率,終端裝置的多樣性亟需更多的移動開發人員的參與。據業內統計,Android研發人才缺口至少30萬。目前,對Android人才需求一類是偏向硬體驅動的Android人才需求,一類是偏向軟體應用的Android人才需求。總的來說,對有志於從事Android硬體驅動的開發工程師來說,現在是一個大展拳腳的機會。那麼,就讓我們一起來看看如何為Android系統編寫核心驅動程式吧。
《Android系統原始碼情景分析》一書正在進擊的程式設計師網(http://0xcc0xcd.com)中連載,點選進入!
這裡,我們不會為真實的硬體裝置編寫核心驅動程式。為了方便描述為Android系統編寫核心驅動程式的過程,我們使用一個虛擬的硬體裝置,這個裝置只有一個4位元組的暫存器,它可讀可寫。想起我們第一次學習程式語言時,都喜歡用“Hello, World”作為例子,這裡,我們就把這個虛擬的裝置命名為“hello”,而這個核心驅動程式也命名為hello驅動程式。其實,Android核心驅動程式和一般Linux核心驅動程式的編寫方法是一樣的,都是以Linux模組的形式實現的,具體可參考前面
二. 進入到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, size_t count, 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, struct device_attribute* attr, 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, char __user *buf, size_t count, 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, const char __user *buf, size_t count, 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, const char* buf, 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, struct device_attribute* attr, 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, struct device_attribute* attr, const char* buf, size_t count) {
struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);
return __hello_set_val(hdev, buf, 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, 0, 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);
}
最後,定義模組載入和解除安裝方法,這裡只要是執行設備註冊和初始化操作:
/*初始化裝置*/
static int __hello_setup_dev(struct hello_android_dev* dev) {
int err;
dev_t devno = MKDEV(hello_major, hello_minor);
memset(dev, 0, sizeof(struct hello_android_dev));
cdev_init(&(dev->dev), &hello_fops);
dev->dev.owner = THIS_MODULE;
dev->dev.ops = &hello_fops;
/*註冊字元裝置*/
err = cdev_add(&(dev->dev),devno, 1);
if(err) {
return err;
}
/*初始化訊號量和暫存器val的值*/
init_MUTEX(&(dev->sem));
dev->val = 0;
return 0;
}
/*模組載入方法*/
static int __init hello_init(void){
int err = -1;
dev_t dev = 0;
struct device* temp = NULL;
printk(KERN_ALERT"Initializing hello device.\n");
/*動態分配主裝置和從裝置號*/
err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME);
if(err < 0) {
printk(KERN_ALERT"Failed to alloc char dev region.\n");
goto fail;
}
hello_major = MAJOR(dev);
hello_minor = MINOR(dev);
/*分配helo裝置結構體變數*/
hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL);
if(!hello_dev) {
err = -ENOMEM;
printk(KERN_ALERT"Failed to alloc hello_dev.\n");
goto unregister;
}
/*初始化裝置*/
err = __hello_setup_dev(hello_dev);
if(err) {
printk(KERN_ALERT"Failed to setup dev: %d.\n", err);
goto cleanup;
}
/*在/sys/class/目錄下建立裝置類別目錄hello*/
hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME);
if(IS_ERR(hello_class)) {
err = PTR_ERR(hello_class);
printk(KERN_ALERT"Failed to create hello class.\n");
goto destroy_cdev;
}
/*在/dev/目錄和/sys/class/hello目錄下分別建立裝置檔案hello*/
temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);
if(IS_ERR(temp)) {
err = PTR_ERR(temp);
printk(KERN_ALERT"Failed to create hello device.");
goto destroy_class;
}
/*在/sys/class/hello/hello目錄下建立屬性檔案val*/
err = device_create_file(temp, &dev_attr_val);
if(err < 0) {
printk(KERN_ALERT"Failed to create attribute val.");
goto destroy_device;
}
dev_set_drvdata(temp, hello_dev);
/*建立/proc/hello檔案*/
hello_create_proc();
printk(KERN_ALERT"Succedded to initialize hello device.\n");
return 0;
destroy_device:
device_destroy(hello_class, dev);
destroy_class:
class_destroy(hello_class);
destroy_cdev:
cdev_del(&(hello_dev->dev));
cleanup:
kfree(hello_dev);
unregister:
unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);
fail:
return err;
}
/*模組解除安裝方法*/
static void __exit hello_exit(void) {
dev_t devno = MKDEV(hello_major, hello_minor);
printk(KERN_ALERT"Destroy hello device.\n");
/*刪除/proc/hello檔案*/
hello_remove_proc();
/*銷燬裝置類別和裝置*/
if(hello_class) {
device_destroy(hello_class, MKDEV(hello_major, hello_minor));
class_destroy(hello_class);
}
/*刪除字元裝置和釋放裝置記憶體*/
if(hello_dev) {
cdev_del(&(hello_dev->dev));
kfree(hello_dev);
}
/*釋放裝置號*/
unregister_chrdev_region(devno, 1);
}
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("First Android Driver");
module_init(hello_init);
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驅動。 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驅動程式互動,敬請期待。