這是一個系列文章:
該系列能對android的從下到上的各層有一個簡單的設(shè)計台汇,麻雀雖小幕袱,五臟俱全植影。enjoy now!
系列文章按照羅升陽大神的blog進行實踐.向羅大神致敬!實現(xiàn)的是在從驅(qū)動層,HAL層,系統(tǒng)添加,JNI方法添加,AIDL方法添加,配置SELinux策略.由于android版本的差異,部分代碼和配置需要修改,kernel和android源碼版本不一樣才能出錯,這樣才有試錯,學(xué)習(xí)成長的機會.本文是參看<<Android系統(tǒng)源代碼情景分析(修訂版)>>以及羅大神的blog和其他網(wǎng)友的blog而來.本系列文章一共5篇塑娇,對應(yīng)書中的第二章,做了對代碼的適配工作以及自己的一點淺薄理解。
平臺: Android N + kernel 3.18 + MTK
android/kernel-3.18/drivers/
----freg
----freg.c
----freg.h
----Kconfig
----Makefile
freg.c
nclude <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 <linux/slab.h>
#include <linux/seq_file.h>
#include <asm/uaccess.h>
#include "freg.h"
static int freg_major = 0;
static int freg_minor = 0;
static struct class* freg_class = NULL;
static struct fake_reg_dev* freg_dev = NULL;
static int freg_open(struct inode* inode, struct file* filp);
static int freg_release(struct inode* inode, struct file* filp);
static ssize_t freg_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos);
static ssize_t freg_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos);
static struct file_operations freg_fops = {
.owner = THIS_MODULE,
.open = freg_open,
.release = freg_release,
.read = freg_read,
.write = freg_write,
};
#define SEQ_printf(m, x...) \
do { \
if (m) \
seq_printf(m, x); \
else \
pr_err(x); \
} while (0)
static int freg_proc_show(struct seq_file *m, void *v)
{
SEQ_printf(m, "%d\n", freg_dev->val);
return 0;
}
static int freg_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, freg_proc_show, inode->i_private);
}
static ssize_t __freg_set_val(struct fake_reg_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;
}
/*
static ssize_t freg_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data){
if(off >0 ){
*eof = 1;
return 0;
}
return __freg_get_val(freg_dev, page);
}*/
static ssize_t freg_proc_write(struct file *filp, const char *ubuf, size_t cnt, loff_t *data){
int err = 0;
char* page = NULL;
if(cnt > PAGE_SIZE){
printk(KERN_ALERT"The buff is too large: %lu.\n", cnt);
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, ubuf, cnt)){
printk(KERN_ALERT"Failed to copy buff from user.\n");
err = -EFAULT;
goto out;
}
err = __freg_set_val(freg_dev, page, cnt);
out:
free_page((unsigned long)page);
return err;
}
static const struct file_operations freg_proc_fops = {
.open = freg_proc_open,
.write = freg_proc_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static ssize_t freg_val_show(struct device* dev, struct device_attribute* attr, char* buf);
static ssize_t freg_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count);
static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, freg_val_show, freg_val_store);
static int freg_open(struct inode * inode, struct file * filp){
struct fake_reg_dev * dev;
dev = container_of(inode->i_cdev, struct fake_reg_dev, dev);
filp->private_data = dev;
return 0;
}
static int freg_release(struct inode* inode, struct file* filp){
return 0;
}
static ssize_t freg_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos){
ssize_t err = 0;
struct fake_reg_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 freg_write(struct file * filp, const char __user * buf, size_t count, loff_t * f_pos){
struct fake_reg_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;
}
static ssize_t __freg_get_val(struct fake_reg_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);
}
static ssize_t freg_val_show(struct device* dev, struct device_attribute* attr, char* buf){
struct fake_reg_dev* hdev = (struct fake_reg_dev*)dev_get_drvdata(dev);
return __freg_get_val(hdev, buf);
}
static ssize_t freg_val_store(struct device*dev, struct device_attribute* attr, const char* buf, size_t count){
struct fake_reg_dev* hdev = (struct fake_reg_dev*)dev_get_drvdata(dev);
return __freg_set_val(hdev, buf, count);
}
static void freg_create_proc(void){
proc_create(FREG_DEVICE_PROC_NAME, 0644, 0, &freg_proc_fops);
}
static void freg_remove_proc(void){
remove_proc_entry(FREG_DEVICE_PROC_NAME, NULL);
}
static int __freg_setup_dev(struct fake_reg_dev* dev){
int err;
dev_t devno = MKDEV(freg_major, freg_minor);
memset(dev, 0, sizeof(struct fake_reg_dev));
cdev_init(&(dev->dev), &freg_fops);
dev->dev.owner = THIS_MODULE;
dev->dev.ops = &freg_fops;
err = cdev_add(&(dev->dev), devno, 1);
if(err){
return err;
}
//init_MUTEX(&(dev->sem));
sema_init(&(dev->sem), 1);
dev->val = 0;
return 0;
}
static int __init freg_init(void){
int err = -1;
dev_t dev = 0;
struct device* temp = NULL;
printk(KERN_ALERT"Initializing freg device.\n");
err = alloc_chrdev_region(&dev, 0, 1, FREG_DEVICE_NODE_NAME);
if(err < 0){
printk(KERN_ALERT"Failed to alloc char dev region.\n");
goto fail;
}
freg_major = MAJOR(dev);
freg_minor = MINOR(dev);
freg_dev = kmalloc(sizeof(struct fake_reg_dev), GFP_KERNEL);
if(!freg_dev){
err = -ENOMEM;
printk(KERN_ALERT"Failed to alloc freg device.\n");
goto unregister;
}
err = __freg_setup_dev(freg_dev);
if(err){
printk(KERN_ALERT"Failed to setup freg device: %d.\n", err);
goto cleanup;
}
freg_class = class_create(THIS_MODULE, FREG_DEVICE_CLASS_NAME);
if(IS_ERR(freg_class)){
err = PTR_ERR(freg_class);
printk(KERN_ALERT"Failed to create freg device class.\n");
goto destroy_cdev;
}
temp = device_create(freg_class, NULL, dev, NULL, "%s", FREG_DEVICE_FILE_NAME);
if(IS_ERR(temp)){
err = PTR_ERR(temp);
printk(KERN_ALERT"Failed to create freg device.\n");
goto destroy_class;
}
err = device_create_file(temp, &dev_attr_val);
if(err < 0){
printk(KERN_ALERT"Failed to create attribute val of freg device.\n");
goto destroy_device;
}
dev_set_drvdata(temp, freg_dev);
freg_create_proc();
printk(KERN_ALERT"Succedded to initialize freg device.\n");
return 0;
destroy_device:
device_destroy(freg_class, dev);
destroy_class:
class_destroy(freg_class);
destroy_cdev:
cdev_del(&(freg_dev->dev));
cleanup:
kfree(freg_dev);
unregister:
unregister_chrdev_region(MKDEV(freg_major, freg_minor), 1);
fail:
return err;
}
static void __exit freg_exit(void){
dev_t devno = MKDEV(freg_major, freg_minor);
printk(KERN_ALERT"Destory freg device.\n");
freg_remove_proc();
if(freg_class){
device_destroy(freg_class, MKDEV(freg_major, freg_minor));
class_destroy(freg_class);
}
if(freg_dev){
cdev_del(&(freg_dev->dev));
kfree(freg_dev);
}
unregister_chrdev_region(devno, 1);
}
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Fake Register Driver");
module_init(freg_init);
module_exit(freg_exit);
在kernel添加一個寄存器的驅(qū)動程序,具體代碼的含義可以參考羅升陽的blog.
freg.h
#ifndef _FAKE_REG_H_
#define _FAKE_REG_H_
#include <linux/cdev.h>
#include <linux/semaphore.h>
#define FREG_DEVICE_NODE_NAME "freg"
#define FREG_DEVICE_FILE_NAME "freg"
#define FREG_DEVICE_PROC_NAME "freg"
#define FREG_DEVICE_CLASS_NAME "freg"
struct fake_reg_dev {
int val;
struct semaphore sem;
struct cdev dev;
};
#endif
Kconfig
config FREG
tristate "Fake Register Driver"
default y
help
This is the freg driver for android system.
這里設(shè)置為default y
是讓內(nèi)核默認(rèn)是編譯的,若果設(shè)置成n就是默認(rèn)不編譯.按照羅大神的單編kernel沒有成功,我有機器,沒有使用模擬器
Makefile
obj-$(CONFIG_FREG) += freg.o
修改android/kernel-3.18/drivers
下面的兩個文件Kconfig和Makefile
在Kconfig文件
menu "Device Drivers"
下添加:
source "drivers/freg/Kconfig"
然后在android目錄下進行make編譯.如果在kernel-3.18目錄下make過可能會導(dǎo)致整編不過,此時需要在把kernel-3.18下的目錄下的.config文件刪除.
編譯成功進行刷機,adb shell
進入機器,就可以看到里面有
proc/freg
sys/class/freg
dev/freg
dev查看需要root權(quán)限,可以先把機器root.dev目錄下的訪問需要權(quán)限,實現(xiàn)在添加HAL以及add到system_server時也會遇到SELinux的限制.這些在后續(xù)的相關(guān)文章有介紹.