1. 程式人生 > >第六期 基於模擬器的Helloworld 核心驅動 《手機就是開發板》

第六期 基於模擬器的Helloworld 核心驅動 《手機就是開發板》

https://blog.csdn.net/aggresss/article/details/53557699

這一期我們來做一個給核心新增驅動的實驗,為了編譯方便,我將android kernel 檔案拷貝到AOSP目錄下,修改目錄名為kernel3.4,在kernel3.4的 drivers 目錄下新建hello資料夾,我將新增的內容都放到了github上:http://github.com/aggresss/PHDemo 。裡面的Codes/hello_KernelDriver 目錄下 或者直接訪問 https://github.com/aggresss/PHDemo/tree/master/Code/hello_KernelDriver
新增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.c 檔案

/*******************************************
*include file and define functions
*******************************************/
#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);
 
/*******************************************
*define traditional file access 
*******************************************/
/*開啟裝置方法*/  
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;  
}  
 
/*******************************************
*define devfs access
*******************************************/
/*讀取暫存器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);  
}  
 
/*******************************************
*define proc access
*******************************************/
/*讀取裝置暫存器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->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);  
}  
 
/*******************************************
*define load and remove function
*******************************************/
/*初始化裝置*/  
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的值*/  
    sema_init(&(dev->sem), 1);  
    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/目錄建立裝置檔案hello,在/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);  


新增 Kconfig 檔案

 config HELLO
           tristate "First Android Driver"
           default n
           help
           This is the first android driver.


新增 Makefile 檔案

obj-$(CONFIG_HELLO) += hello.o


修改 drivers/Kconfig 檔案 在menu "Device Drivers"和endmenu之間新增一行:

source "drivers/hello/Kconfig"


這樣,執行make menuconfig時,就可以配置hello模組的編譯選項了。

修改drivers/Makefile檔案,新增一行:

obj-$(CONFIG_HELLO) += hello/


程式碼部分修改完畢後,進入到AOSP編譯的根目錄,執行

source ./build/envsetup.sh
lunch 1


這幾部操作是為了將交叉編譯工具的目錄加入到PATH目錄中,省去我們手工指定的過程。
然後執行:

export ARCH=arm
export CROSS_COMPILE=arm-linux-androideabi-
make goldfish_armv7_defconfig
make menuconfig 


將 First Android Driver 選項 按Y,編譯進核心,儲存退出後執行

make -j8


然後編譯好的核心就儲存在 /kernel3.4/arch/arm/boot/zImage 中,接下來啟動模擬器 emulator -kernel ./kernel3.4/arch/arm/boot/zImage &
模擬器成功啟動後,執行

adb shell 


就進入到了android 模擬器的 shell 中了,這是 ls /dev 就能看到 hello 的裝置檔案。
下面來驗證一下我們新增的虛擬裝置,這個裝置有一個int型別的暫存器,是通過操作記憶體模擬出來的,真正的裝置是操作SOC設計時分配的實際地址資源。
有三種方式來驗證,我們的裝置是否正常:
1. /dev 檔案系統
2. /proc 檔案系統
3. /sys 檔案系統
其中 /dev 檔案系統需要通過系統呼叫來操作,我們放到下一期製作一個小的Demo可執行程式來驗證,下面我們使用 /proc和/sys檔案系統來驗證:

通過/proc檔案系統驗證:
echo "666" > /proc/hello
cat /proc/hello
通過/sys檔案系統驗證:
echo "666" > /sys/class/hello/hello/val
cat /sys/class/hello/hello/val


如果以上兩組測試都返回 666 則說明我們的虛擬裝置的驅動程式已經成功載入。