为打通任督二脉而整理。

参考自《Android系统源代码情景分析》


环境:

MTK方案 6.0代码

kernel-3.18

ubuntu 12.04


一、开发Android硬件驱动程序

kernel-3.18/drivers/misc/mediatek/freg/freg.h

kernel-3.18/drivers/misc/mediatek/freg/freg.c

kernel-3.18/drivers/misc/mediatek/freg/Makefile

kernel-3.18/drivers/misc/mediatek/Makefile


system\core\rootdir\ueventd.rc


kernel-3.18/drivers/misc/mediatek/freg/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


kernel-3.18/drivers/misc/mediatek/freg/freg.c


#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 <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); 
} 

/*把缓冲区buf的值写到设备寄存器val中,内部使用*/
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;
}

/*
//读取设备寄存器val的值,保存到page缓冲区中
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);
}*/

//把缓冲区的值buff到设备寄存器val中
//static ssize_t freg_proc_write(struct file* filp, const char __user *buff, unsigned long len, void* data){
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;
}

//proc/freg操作方法表
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, 
};


/*devfs文件系统的设备属性操作方法*/
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);

/*devfs文件系统的设备属性*/
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;
}

/*读取设备的寄存器val值*/
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;
	}

	/*将寄存器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 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;
}

/*将寄存器val的值读取到缓冲区buf中,内部使用*/
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);
}


/*读设备属性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);
}

/*写设备属性val的值*/
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);
}


/*创建/proc/freg文件*/
static void freg_create_proc(void){
	proc_create(FREG_DEVICE_PROC_NAME, 0644, 0,  &freg_proc_fops); 
}

/*删除/proc/freg文件*/
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;
	}

	/*初始化信号量和寄存器val的值*/
	//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设备结构体*/
	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;
	}

	/*在/sys/class/目录下创建设备类别目录freg*/
	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;
	}

	/*在/dev/目录和/sys/class/freg目录下分别创建设备文件freg*/
	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;
	}

	/*在/sys/class/freg/freg目录下创建属性文件val*/
	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);

	/*创建/proc/freg文件*/
	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");

	/*删除/proc/freg文件*/
	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-3.18/drivers/misc/mediatek/freg/Makefile

obj-y += freg.o


kernel-3.18/drivers/misc/mediatek/Makefile

添加

obj-y   += freg/


二、开发C可执行程序验证Android硬件驱动程序

external/freg/freg.c

external/freg/Android.mk


external/freg/Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := freg
LOCAL_SRC_FILES := $(call all-subdir-c-files)
include $(BUILD_EXECUTABLE)

external/freg/freg.c

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#define FREG_DEVICE_NAME "/dev/freg"

int main(int argc, char** argv){
	int fd = -1;
	int val = 0;

	fd = open(FREG_DEVICE_NAME, O_RDWR);
	if(fd == -1){
		printf("Failed to open device %s.\n", FREG_DEVICE_NAME);
		return -1;		
	}

	printf("Read original value:\n");
	read(fd, &val, sizeof(val));
	printf("%d.\n\n", val);

	val = 5;
	printf("Write value %d to %s.\n\n", val, FREG_DEVICE_NAME);
	write(fd, &val, sizeof(val));
	printf("Read the value again:\n");
	read(fd, &val, sizeof(val));
	printf("%d.\n\n", val);
	close(fd);

	return 0;
}




三、验证内核驱动程序模块

1、验证/dev/freg

root@XXXX:/ # freg
freg
Read original value:
0.

Write value 5 to /dev/freg.

Read the value again:
5.


2、验证/proc/freg

root@XXXX:/proc #  echo '0' > freg
 echo '0' > freg
root@C5011:/proc # cat freg
cat freg
0
root@XXXX:/proc #



3、验证/sys/class/freg/freg

root@XXXX:/ # cd sys/class/freg/freg
cd sys/class/freg/freg
root@XXXX:/sys/class/freg/freg # ls
ls
dev
power
subsystem
uevent
val
root@XXXX:/sys/class/freg/freg # cat val
cat val
0
root@XXXX:/sys/class/freg/freg # echo '5' > val
echo '5' > val
root@XXXX:/sys/class/freg/freg # cat val
cat val
5
root@XXXX:/sys/class/freg/freg #



四、处理硬件设备访问权限问题

root@XXXX:/ # ls -l /dev/freg
ls -l /dev/freg
crw------- root     root     232,   0 2010-01-01 08:00 freg

root@XXXX:/ # ls -l /sys/class/freg/freg/val
ls -l /sys/class/freg/freg/val
-rw-r--r-- root     root         4096 2016-01-01 00:00 val

root@XXXX:/ # ls -l /proc/freg
ls -l /proc/freg
-rw-r--r-- root     root            0 2016-01-01 04:40 freg
root@XXXX:/ #


/dev/freg的访问权限只有root可以读写,我们需要修改其权限。

system\core\rootdir\ueventd.rc

最后一行添加,然后就整个工程编译一下吧,只要你编译过了再重新编译一次也不用花多少时间的。

/dev/freg                     0666  root        root