在Ubuntu 上為Android 系統編寫Linux 內核驅動程序
這里,我們不會為真實的硬件設備編寫內核驅動程序。為了方便描述為Android 系統編寫內核驅動程序的過程,我們使用一
個虛擬的硬件設備,這個設備只有一個4 字節的寄存器,它可讀可寫。想起我們第一次學習程序語言時,都喜歡用“Hello, World”
作為例子,這里,我們就把這個虛擬的設備命名為“hello”,而這個內核驅動程序也命名為hello 驅動程序。其實,Android 內
核驅動程序和一般Linux 內核驅動程序的編寫方法是一樣的,都是以Linux 模塊的形式實現的,具體可參考前面Android 學習
啟動篇一文中提到的Linux Device Drivers 一書。不過,這里我們還是從Android 系統的角度來描述Android 內核驅動程序的編
寫和編譯過程。
一. 因為手邊的硬件是Galaxy Nexus(maguro),下載和編譯code詳見 http://wiki.cyanogenmod.org/w/Maguro_Info
客官會問,為啥你要這樣折騰啊?因為CM會把kernel的code一起編進來,不要再單獨編譯kernel。
二. 進入到kernel/drivers 目錄,新建hello 目錄,然后文件創建hello.h, hello.c, Makefile文件
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" #endif
hello.c
#include <linux/module.h> #include <linux/init.h> #include <linux/types.h> #include <linux/fs.h> #include <linux/proc_fs.h> #include <linux/device.h> #include <asm/uaccess.h> #include <linux/cdev.h> #include <linux/semaphore.h> #include "hello.h" struct hello_android_dev { int val; struct semaphore sem; struct cdev dev; }; 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 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); 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 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; } static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos) { printk("hello_read()\n"); 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; } if(copy_to_user(buf, &(dev->val), sizeof(dev->val))) { err = -EFAULT; goto out; } err = sizeof(dev->val); out: up(&(dev->sem)); return err; } 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; printk("hello_write()\n"); 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; } static ssize_t __hello_get_val(struct hello_android_dev* dev, char* buf) { printk("__hello_get_val()\n"); int val = 0; struct semaphore *p_sem = &(dev->sem); if(down_interruptible(p_sem)) { printk("sem is used,pls wait to free.\n"); return -ERESTARTSYS; } val = dev->val; up(p_sem); printk("get val: %d\n",val); return snprintf(buf, PAGE_SIZE, "%d\n", val); } static ssize_t __hello_set_val(struct hello_android_dev* dev, const char* buf, size_t count) { printk("__hello_set_val()\n"); int val = 0; val = simple_strtol(buf, NULL, 10); printk("set val: %d\n",val); if(down_interruptible(&(dev->sem))) { return -ERESTARTSYS; } dev->val = val; up(&(dev->sem)); return count; } static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf) { printk("hello_val_show()\n"); struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev); return __hello_get_val(hdev, buf); } static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) { printk("hello_val_store()\n"); struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev); return __hello_set_val(hdev, buf, count); } static ssize_t hello_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data) { printk("hello_proc_read()\n"); if(off > 0) { *eof = 1; return 0; } return __hello_get_val(hello_dev, page); } static ssize_t hello_proc_write(struct file* filp, const char __user *buff, unsigned long len, void* data) { printk("hello_proc_write()\n"); 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; } static void hello_remove_proc(void) { remove_proc_entry(HELLO_DEVICE_PROC_NAME, NULL); } 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 has no "owner" member entry->read_proc = hello_proc_read; entry->write_proc = hello_proc_write; } } static int __hello_setup_dev(struct hello_android_dev* dev) { int err; dev_t devno = MKDEV(hello_major, hello_minor); printk("__hello_setup_dev()\n"); 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; } //init_MUTEX(&(dev->sem)); 2.6.25及以后的linux內核版本廢除了init_MUTEX函數 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);
// 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; } 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; } 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; } 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); 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"); 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);
kernel/drivers/hello/Makefile
obj-y += hello.o
三、修改drivers/Makefile 文件,添加一行:
obj-$(CONFIG_HELLO) += hello/
四、驗證測試
驗證hello 驅動程序是否已經正常安裝:
linuxidc@www.linuxdc.com:~/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