Android应用程序访问linux驱动第一步:实现并测试Linux驱动

一直都想亲自做一次使用android应用程序访问Linux内核驱动的尝试,但总是没能做到。最近抽出时间,下决心重新尝试一次。尝试的开始当然是先写一个Linux内核驱动了。
我希望写一个简单测驱动程序,实现写一个字符串进去,然后再把它读出来的功能。驱动中会创建dev/hello设备节点和/sys/class/hello/hello/val 设备节点,没有实现proc/下的对应的设备节点。/sys/class/hello/hello/val 主要用于快速测试,而dev/hello则主要用于供上层应用调用。
这篇博客参考了老罗的android之旅相关博客:http://blog.csdn.net/luoshengyang/article/details/6568411

一。驱动源码

代码我已经在android6.0的linux kernel上测试过了,代码中有响应的注释,所以这里直接贴出代码:

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 <linux/sched.h>
#include <linux/errno.h>
#include <linux/fcntl.h>

#include <linux/poll.h>
#include <linux/seq_file.h>
#include <linux/mutex.h>
#include <linux/workqueue.h>

#include <asm/uaccess.h>
#include <linux/slab.h>


#include "hello.h"  

/*定义主设备和从设备号变量*/  
static int hello_major = 0;  
static int hello_minor = 0;  

/*设备类别和设备变量*/  
static struct class* hello_class = NULL;  
static struct hello_test_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); 

/*打开设备方法*/  
static int hello_open(struct inode* inode, struct file* filp) {  
    struct hello_test_dev* dev;          

    /*将自定义设备结构体保存在文件指针的私有数据域中,以便访问设备时拿来用*/  
    dev = container_of(inode->i_cdev, struct hello_test_dev, dev);  
    filp->private_data = dev;  

    return
  • 9
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值