老罗android之旅补丁版之一

声明:本篇仅是自己学习老罗android之旅时的心得体会及对部分内容的更新,与老罗本人博客无任何直接关系

先贴出老罗原版学习计划网址http://blog.csdn.net/luoshengyang/article/details/6567257

因为他的博客写的时间已经较久,所以有部分内容已经发生变化,在当前android版本已经不适用

因为做这个实验已有一周多,总结了一下弯路,希望大家学习时可以参考

1>首先是第一篇

在Android内核源代码工程中编写硬件驱动程序

其中构建了三条通路,但就构建方法而言,其中有一条proc路线是不通的,我直接把这块注释掉了

如果想去实现肯定也是可以的,只是需要自己去查现在的proc头文件内容,然后根据现在变化后的函数参数去实现原来的函数功能,

如果不知道怎么用,搜搜看别人在其他文件夹怎么用的就好了,然后照猫画虎。

同样是在kernel/drivers/下新建hello目录,

①添加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<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<linux/slab.h>

#include "hello.h"

/*1>定义三种访问设备的方法*/
//主设备号和从设备号变量
static int hello_major = 0;//main device
static int hello_minor = 0;//follow device

//设备类别和设备变量
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);

/*2>定义传统的设备文件访问方法,open,release,read,write打开,释放,读写设备文件的方法*/
//打开设备
static int hello_open(struct inode* inode,struct file* filp)
{
    struct hello_android_dev* dev;

    //将自定义设备结构体保存在文件private域
    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的值copy到用户提供的缓冲区
    if(copy_to_user(buf,&(dev->val),sizeof(dev->val)))
    {
        err = -EFAULT;
        goto out;
    }
    err = sizeof(dev->val);

out:
     up(&(dev->sem));
     return err;
}

//write设备的寄存器值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;
}

/*通过devfs文件系统访问
把设备寄存器val看作设备属性,通过读写属性对设备进行访问
主要是实现hello_val_show和hello_val_store,
同时定义内部使用的访问val方法,__hello_get_val和__hello_set_val*/
//读取寄存器的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));

    printk("jessietest~~kernel~~hello_show->__hello_get_val\n");
    return snprintf(buf,PAGE_SIZE,"%d\n",val);
    //snprintf,将val及其后参数按照"%d\n"格式转化成字符串,然后复制到buf,长度为PAGE_SIZE
}
//把缓冲区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));

    printk("jessietest~~kernel~~hello_store->__hello_set_val\n");

    return count;
}

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

    printk("jessietest~~kernel~~hello_show\n");

    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)
{
    struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);

    printk("jessietest~~kernel~~hello_store\n");

    return __hello_set_val(hdev,buf,count);
}

/*定义通过proc文件系统访问
主要实现了hello_proc_read和hello_proc_write
定义了在proc文件系统创建和删除文件的hello_create_proc和hello_remove_proc*/
//读取设备寄存器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()
{
    struct proc_dir_entry* entry;

    entry = create_proc_entry(HELLO_DEVICE_PROC_NAME,0,NULL);
    if(entry)
    {
        entry->owner = THIS_MODULE;
        entry->read_proc = hello_proc_read;
        entry->write_proc = hello_proc_write;
    }
}
//删除/proc/hello文件
static void hello_remove_proc()
{
    remove_proc_entry(HELLO_DEVICE_PROC_NAME,NULL);
}
*/

/*定义模块加载和卸载方法,这里只是执行设备注册和初始化操作*/
//初始化设备
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);

    //分配hello设备结构体变量
    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/目录和/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"Succeeded 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);
可以看到我已经把proc相关的东西注释掉了,

③下面添加make文件,Makefile

obj-y	+=hello.o
注意,重点来了,在老罗的实验里,是先添加Kconfig文件,Kconfig文件内容是这样的

config HELLO

           tristate "First Android Driver"
           default n
           help
           This is the first android driver.
Makefile文件内容是这样的
obj-$(CONFIG_HELLO) += hello.o

虽然我不完全理解,但是大意是这样的,在menu中生成一个First Android Driver选项,通过Kconfig文件内容,

就可以生成一个CONFIG_HELLO,然后在make文件的obj-$(CONFIG_HELLO) += hello.o这句话就是,

根据Kconfig文件生成的CONFIG_HELLO来条件编译生成hello.o文件,

其中还有一步是执行make menuconfig,去改变First Android Driver的值为Y,而Y的意思就是强制编译,

也就是通过make menuconfig变条件编译为强制编译,也许这样是为了增强适用性吧

然而对于目前而言,make menuconfig操作的效果就是会在kernel下生成一个.config的东西,导致下次编译的时候会报错,显示kernel not clean

这也是我琢磨了好久才发现的,也就是说只要执行make menuconfig命令,就会生成.config从而导致编译失败,一定注意,不要去执行make menuconfig

所以现在这里的Kconfig文件已经没有意义了,可以删掉

④同理,drivers/Makefile文件

obj-$(CONFIG_HELLO) += hello/

改为

obj-y          +=hello/
这样就OK了

因为修改kernel内容自然是要重新编译生成boot.image并烧录的


在dev/下看结果

可以看到字符设备(c是字符设备的意思)hello,已经生成在/dev下了


Thanks~~待续~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值