Demo先行。
使用之前所编译Android4.4.2源码及Kernel3.4.67版正常运行
然后,
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ emulator -kernel /homsenrsl/android/source/kernel/goldfish/arch/arm/boot/zImage
1,编写Linux内核驱动程序
①创建定义驱动的目录及文件
- senrsl@senrsl-ubuntu:~$ cd android/source/kernel/goldfish/drivers/
- senrsl@senrsl-ubuntu:~/android/source/kernel/goldfish/drivers$ mkdir senrsl
- senrsl@senrsl-ubuntu:~/android/source/kernel/goldfish/drivers$ cd senrsl
- senrsl@senrsl-ubuntu:~/android/source/kernel/goldfish/drivers/senrsl$ touch senrsl.h
- senrsl@senrsl-ubuntu:~/android/source/kernel/goldfish/drivers/senrsl$ touch senrsl.c
- senrsl@senrsl-ubuntu:~/android/source/kernel/goldfish/drivers/senrsl$ touch Kconfig
- senrsl@senrsl-ubuntu:~/android/source/kernel/goldfish/drivers/senrsl$ touch Makefile
1)senrsl.h
- #ifndef _SENRSL_ANDROID_H_
- #define _SENRSL_ANDROID_H_
- #include <linux/cdev.h>
- #include <linux/semaphore.h>
- //定义字符串常量宏
- #define SENRSL_DEVICE_NODE_NAME "senrsl"
- #define SENRSL_DEVICE_FILE_NAME "senrsl"
- #define SENRSL_DEVICE_PROC_NAME "senrsl"
- #define SENRSL_DEVICE_CLASS_NAME "senrsl"
- //字符设备结构体 senrsl_android_dev,即虚拟设备
- struct senrsl_android_dev {
- int val;//代表设备里的寄存器
- struct semaphore sem;//信号量,用于同步访问寄存器val
- struct cdev dev;//内嵌字符设备,Linux驱动程序自定 义字符设备结构体的标准方法
- };
- #endif
- /**
- * senrsl.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 "senrsl.h"
- /*主设备和从设备号变量*/
- static int senrsl_major = 0;
- static int senrsl_minor = 0;
- /*设备类别和设备变量*/
- static struct class* senrsl_class = NULL;
- static struct senrsl_android_dev* senrsl_dev = NULL;
- /*传统的设备文件操作方法*/
- static int senrsl_open(struct inode* inode, struct file* filp);
- static int senrsl_release(struct inode* inode, struct file* filp);
- static ssize_t senrsl_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos);
- static ssize_t senrsl_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos);
- /*设备文件操作方法表*/
- static struct file_operations senrsl_fops = {
- .owner = THIS_MODULE,
- .open = senrsl_open,
- .release = senrsl_release,
- .read = senrsl_read,
- .write = senrsl_write,
- };
- /*访问设置属性方法*/
- static ssize_t senrsl_val_show(struct device* dev, struct device_attribute* attr, char* buf);
- static ssize_t senrsl_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count);
- /*定义设备属性*/
- static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, senrsl_val_show, senrsl_val_store);
- /******************** 定义传统的设备文件访问方法,主要是定义senrsl_open、senrsl_release、senrsl_read和 senrsl_write这四个打开、释放、读和写设备文件的方法*************************/
- /*打开设备方法*/
- static int senrsl_open(struct inode* inode, struct file* filp) {
- struct senrsl_android_dev* dev;
- /*将自定义设备结构体保存在文件指针的私 有数据域中,以便访问设备时拿来用*/
- dev = container_of(inode->i_cdev, struct senrsl_android_dev, dev);
- filp->private_data = dev;
- return 0;
- }
- /*设备文件释放时调用,空实现*/
- static int senrsl_release(struct inode* inode, struct file* filp) {
- return 0;
- }
- /*读取设备的寄存器val的值*/
- static ssize_t senrsl_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos) {
- ssize_t err = 0;
- struct senrsl_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 senrsl_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos) {
- struct senrsl_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看成是设备的一个属性,通过读写这个属性来对设备进行访问,主要是实 现senrsl_val_show和senrsl_val_store两个方法,同时定义了两个内部使用的访问val值的方法 __senrsl_get_val和 __senrsl_set_val****************************************/
- /*读取寄存器val的值到缓冲区buf 中,内部使用*/
- static ssize_t __senrsl_get_val(struct senrsl_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 __senrsl_set_val(struct senrsl_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 senrsl_val_show(struct device* dev, struct device_attribute* attr, char* buf) {
- struct senrsl_android_dev* hdev = (struct senrsl_android_dev*)dev_get_drvdata(dev);
- return __senrsl_get_val(hdev, buf);
- }
- /*写设备属性val*/
- static ssize_t senrsl_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) {
- struct senrsl_android_dev* hdev = (struct senrsl_android_dev*)dev_get_drvdata(dev);
- return __senrsl_set_val(hdev, buf, count);
- }
- /***********定义通过proc 文件系统访问方法,主要实现了senrsl_proc_read和senrsl_proc_write两个方法,同时定义了在 proc文件系统创建和删除文件的方法senrsl_create_proc和 senrsl_remove_proc*****************/
- /*读取设备寄存器val的值,保存在 page缓冲区中*/
- static ssize_t senrsl_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data) {
- if(off > 0) {
- *eof = 1;
- return 0;
- }
- return __senrsl_get_val(senrsl_dev, page);
- }
- /*把缓冲区的值buff保存到设备寄存器 val中去*/
- static ssize_t senrsl_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 = __senrsl_set_val(senrsl_dev, page, len);
- out:
- free_page((unsigned long)page);
- return err;
- }
- /*创建/proc/senrsl文件*/
- static void senrsl_create_proc(void) {
- struct proc_dir_entry* entry;
- entry = create_proc_entry(SENRSL_DEVICE_PROC_NAME, 0, NULL);
- if(entry) {
- //drivers/senrsl/senrsl.c:220:8: error: 'struct proc_dir_entry' has no member named 'owner'
- //这个结构成员更新 为:pde_users
- entry->pde_users = THIS_MODULE;
- entry->read_proc = senrsl_proc_read;
- entry->write_proc = senrsl_proc_write;
- }
- }
- /*删除/proc/senrsl文件*/
- static void senrsl_remove_proc(void) {
- remove_proc_entry(SENRSL_DEVICE_PROC_NAME, NULL);
- }
- /**************最后,定义 模块加载和卸载方法,这里只要是执行设备注册和初始化操作*****************/
- /*初始化设备*/
- static int __senrsl_setup_dev(struct senrsl_android_dev* dev) {
- int err;
- dev_t devno = MKDEV(senrsl_major, senrsl_minor);
- memset(dev, 0, sizeof(struct senrsl_android_dev));
- cdev_init(&(dev->dev), &senrsl_fops);
- dev->dev.owner = THIS_MODULE;
- dev->dev.ops = &senrsl_fops;
- /*注册字符设备*/
- err = cdev_add(&(dev->dev),devno, 1);
- if(err) {
- return err;
- }
- /*初始化信号量和寄存器val的值*/
- //drivers/senrsl/senrsl.c:252:2: error: implicit declaration of function 'init_MUTEX' [-Werror=implicit-function-declaration]
- //init_MUTEX(&(dev->sem));
- sema_init((&dev->sem),1);
- dev->val = 0;
- return 0;
- }
- /*模块加载方法*/
- static int __init senrsl_init(void){
- int err = -1;
- dev_t dev = 0;
- struct device* temp = NULL;
- printk(KERN_ALERT"Initializing senrsl device.\n");
- /*动态分配主设备和从设备号*/
- err = alloc_chrdev_region(&dev, 0, 1, SENRSL_DEVICE_NODE_NAME);
- if(err < 0) {
- printk(KERN_ALERT"Failed to alloc char dev region.\n");
- goto fail;
- }
- senrsl_major = MAJOR(dev);
- senrsl_minor = MINOR(dev);
- /*分配helo设备结构体变量*/
- senrsl_dev = kmalloc(sizeof(struct senrsl_android_dev), GFP_KERNEL);
- if(!senrsl_dev) {
- err = -ENOMEM;
- printk(KERN_ALERT"Failed to alloc senrsl_dev.\n");
- goto unregister;
- }
- /*初始化设备*/
- err = __senrsl_setup_dev(senrsl_dev);
- if(err) {
- printk(KERN_ALERT"Failed to setup dev: %d.\n", err);
- goto cleanup;
- }
- /*在/sys/class/目录下创建设 备类别目录senrsl*/
- senrsl_class = class_create(THIS_MODULE, SENRSL_DEVICE_CLASS_NAME);
- if(IS_ERR(senrsl_class)) {
- err = PTR_ERR(senrsl_class);
- printk(KERN_ALERT"Failed to create senrsl class.\n");
- goto destroy_cdev;
- }
- /*在/dev/目录和/sys /class/senrsl目录下分别创建设备文件senrsl*/
- temp = device_create(senrsl_class, NULL, dev, "%s", SENRSL_DEVICE_FILE_NAME);
- if(IS_ERR(temp)) {
- err = PTR_ERR(temp);
- printk(KERN_ALERT"Failed to create senrsl device.");
- goto destroy_class;
- }
- /*在/sys/class/senrsl /senrsl目录下创建属性文件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, senrsl_dev);
- /*创建/proc/senrsl文件*/
- senrsl_create_proc();
- printk(KERN_ALERT"Succedded to initialize senrsl device.\n");
- return 0;
- destroy_device:
- device_destroy(senrsl_class, dev);
- destroy_class:
- class_destroy(senrsl_class);
- destroy_cdev:
- cdev_del(&(senrsl_dev->dev));
- cleanup:
- kfree(senrsl_dev);
- unregister:
- unregister_chrdev_region(MKDEV(senrsl_major, senrsl_minor), 1);
- fail:
- return err;
- }
- /*模块卸载方法*/
- static void __exit senrsl_exit(void) {
- dev_t devno = MKDEV(senrsl_major, senrsl_minor);
- printk(KERN_ALERT"Destroy senrsl device.\n");
- /*删除/proc/senrsl文件*/
- senrsl_remove_proc();
- /*销毁设备类别和设备*/
- if(senrsl_class) {
- device_destroy(senrsl_class, MKDEV(senrsl_major, senrsl_minor));
- class_destroy(senrsl_class);
- }
- /*删除字符设备和释放设备内存*/
- if(senrsl_dev) {
- cdev_del(&(senrsl_dev->dev));
- kfree(senrsl_dev);
- }
- /*释放设备号*/
- unregister_chrdev_region(devno, 1);
- }
- MODULE_LICENSE("GPL");
- MODULE_DESCRIPTION("First Android Driver");
- module_init(senrsl_init);
- module_exit(senrsl_exit);
3)Kconfig
- config SENRSL
- tristate "First Android Driver With senRsl"
- default n
- help
- This is the first android driver whit me.
4)Makefile
- obj-$(CONFIG_SENRSL) += senrsl.o
- #在Kconfig文件 中,tristate表示编译选项HELLO支持在编译内核时,senrsl模块支持以模块、内建和不编译三种编译方法,默认是不编译, 因此,#在编译内核前,我们还需要执行make menuconfig命令来配置编译选项,使得senrsl可以以模块或者内建的方法进行编译。
- # 在Makefile文件 中,根据选项senrsl的值,执行不同的编译方法。
③配置编译选项
修改当前目录下总署的Kconfig和Makefile配置,加上新建的。
目录:/home/senrsl/android/source/kernel/goldfish/drivers
1)Kconfig 最后面加上
- menu
"Device Drivers"
- 这中间有好多
- source "drivers/virt/Kconfig"
- source "drivers/devfreq/Kconfig"
- source "drivers/senrsl/Kconfig"
- endmenu
2)Makefile拉到最下面加上
- # Virtualization drivers
- obj-$(CONFIG_VIRT_DRIVERS) += virt/
- obj-$(CONFIG_HYPERV) += hv/
- obj-$(CONFIG_SENRSL) += senrsl/
- obj-$(CONFIG_PM_DEVFREQ) += devfreq/
④配置编译
1)配置编译时选项加上
启动图形化配置
- senrsl@senrsl-ubuntu:~/android/source/kernel/goldfish$ make menuconfig
2)执行编译
- senrsl@senrsl-ubuntu:~/android/source/kernel/goldfish$ make
- 。。。。。
- OBJCOPY arch/arm/boot/zImage
- Kernel: arch/arm/boot/zImage is ready
⑤启动验证
使用这个内核启动模拟器
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ emulator -kernel /home/senrsl/android/source/kernel/goldfish/arch/arm/boot/zImage
- senrsl@senrsl-ubuntu:~$ adb shell
- root@generic:/ # cd dev/
- root@generic:/dev # ls | grep s
- __properties__
- ashmem
- console
- fscklogs
- fuse
- graphics
- kmsg
- psaux
- pts
- senrsl
- socket
- vcs
- vcs1
- vcsa
- vcsa1
- root@generic:/dev # cd ..
- root@generic:/ # cd proc
- root@generic:/proc # ls | grep s
- bus
- cgroups
- consoles
- devices
- diskstats
- dma-mappings
- execdomains
- filesystems
- fs
- interrupts
- ioports
- kallsyms
- kmsg
- kpageflags
- locks
- misc
- mounts
- partitions
- sched_debug
- schedstat
- self
- senrsl
- slabinfo
- softirqs
- stat
- swaps
- sys
- sysrq-trigger
- sysvipc
- timer_list
- version
- vmstat
- yaffs
- root@generic:/proc # cat senrsl
- 0
- root@generic:/proc # echo '5'>senrsl
- root@generic:/proc # cat senrsl
- 5
- root@generic:/proc/sys # cd ..
- root@generic:/proc # cd ..
- root@generic:/ # cd sy
- sys/ system/
- root@generic:/ # cd sys/class/
- root@generic:/sys/class # ll
- drwxr-xr-x root root 2015-04-09 23:18 bdi
- drwxr-xr-x root root 2015-04-09 23:18 block
- drwxr-xr-x root root 2015-04-09 23:18 firmware
- drwxr-xr-x root root 2015-04-09 23:18 graphics
- drwxr-xr-x root root 2015-04-09 23:18 input
- drwxr-xr-x root root 2015-04-09 23:18 mem
- drwxr-xr-x root root 2015-04-09 23:18 misc
- drwxr-xr-x root root 2015-04-09 23:18 mmc_host
- drwxr-xr-x root root 2015-04-09 23:18 mtd
- drwxr-xr-x root root 2015-04-09 23:18 net
- drwxr-xr-x root root 2015-04-09 23:18 power_supply
- drwxr-xr-x root root 2015-04-09 23:18 rtc
- drwxr-xr-x root root 2015-04-09 23:18 senrsl
- drwxr-xr-x root root 2015-04-09 23:18 switch
- drwxr-xr-x root root 2015-04-09 23:18 timed_output
- drwxr-xr-x root root 2015-04-09 23:18 tty
- drwxr-xr-x root root 2015-04-09 23:18 vc
- drwxr-xr-x root root 2015-04-09 23:18 vtconsole
- drwxr-xr-x root root 2015-04-09 23:18 xt_idletimer
- root@generic:/sys/class # cd senrsl/
- root@generic:/sys/class/senrsl # ll
- lrwxrwxrwx root root 2015-04-09 23:18 senrsl -> ../../devices/virtual/senrsl/senrsl
- root@generic:/sys/class/senrsl # cd senrsl/
- root@generic:/sys/class/senrsl/senrsl # ll
- -r--r--r-- root root 4096 2015-04-09 23:18 dev
- drwxr-xr-x root root 2015-04-09 23:18 power
- lrwxrwxrwx root root 2015-04-09 23:18 subsystem -> ../../../../class/senrsl
- -rw-r--r-- root root 4096 2015-04-09 23:18 uevent
- -rw-r--r-- root root 4096 2015-04-09 23:18 val
- root@generic:/sys/class/senrsl/senrsl # cat val
- 5
- root@generic:/sys/class/senrsl/senrsl # echo '0' > val
- root@generic:/sys/class/senrsl/senrsl # cat val
- 0
- root@generic:/sys/class/senrsl/senrsl #
搞定!
2015年04月10日13:27:59
--
senRsl
2015年04月08日15:45:13
2015年04月08日15:45:13
没有评论 :
发表评论