ok6410 android driver(2)
I will paste and anlaysis a small character device driver in this paragraph.
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h> #include <linux/unistd.h> #define DEVICE_NAME "wordcount2"
#define TRUE -1
#define FALSE 0 static unsigned char mem[];
static int word_count = ; static char is_spacewhite(char c)
{
if(c == ' ' || c == || c == || c == )
return TRUE;
else
return FALSE;
} static int get_world_count(const char *buf)
{
int n = ;
int i = ;
char c = ' ';
char flag = ; if(*buf == '\0')
return ; if(is_spacewhite(*buf) == TRUE)
n--; for(; (c = *(buf +i)) != '\0'; i++)
{
if(flag == && is_spacewhite(c) == FALSE) {
flag = ;
} else if ( flag == && is_spacewhite(c) == TRUE) {
continue;
} if(is_spacewhite(c) == TRUE) {
n++;
flag =;
}
}
if(is_spacewhite(*(buf + i -)) == TRUE)
n--; return n;
} static ssize_t word_count_read(struct file *filp, char __user *buf,
size_t count, loff_t *ppos)
{
int ret;
unsigned char tmp[]; tmp[] = word_count >> ;
tmp[] = word_count >> ;
tmp[] = word_count >> ;
tmp[] = word_count ; ret = copy_to_user(buf, (void*) tmp, );
printk(KERN_NOTICE "[wordcount2] read count : %d", (int) count + );
printk(KERN_NOTICE "read wordcount success.\n");
return count;
} static ssize_t word_count_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
int ret;
ssize_t written = count; ret = copy_from_user(mem, buf, count);
mem[count] = '\0'; word_count = get_world_count(mem); printk(KERN_NOTICE "[wordcount2] written count : %d", (int)word_count);
printk(KERN_NOTICE "write wordcount success.\n");
return written; } static const struct file_operations misc_fops = {
.owner = THIS_MODULE,
.read = word_count_read,
.write = word_count_write,
}; static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &misc_fops,
}; static int word_count_init(void)
{
int ret;
ret = misc_register(&misc);
printk("word count init success.\n");
return ret;
} static void word_count_exit(void)
{
misc_deregister(&misc);
printk("word count exit success.\n");
} MODULE_LICENSE("Dual BSD/GPL"); module_init(word_count_init);
module_exit(word_count_exit);
1、The entrance of device driver
When we are looking into a device driver, we should find the following entrance functions.
module_init(word_count_init);
module_exit(word_count_exit);
These two functions anounce the init and exit functions :
"word_count_init" and "word_count_exit".
Then we could check what effect of these two functions.
static int word_count_init(void)
{
int ret;
ret = misc_register(&misc);
printk("word count init success.\n");
return ret;
} static void word_count_exit(void)
{
misc_deregister(&misc);
printk("word count exit success.\n");
}
In a device driver, we must got a device.
The device "misc" is what we want.
When we insmod the module, this device would be register into the kernel.
2、Find out what mechanism the device support
------------------------------------------------------------------------------------------------------
I wanna add some messenge in this part :
In linux, we always talk about mechanism and policy.
Mechanism means that the methods of operation what we want.
Policy means that the methos of operation how can we use.
-----------------------------------------------------------------------------------------------------
In the device driver, we frequently support the mechanism instead of policy.
Means we create a device and give the methods of operation but never use it.
The miscdevice struct
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &misc_fops,
}; //
#define DEVICE_NAME "wordcount2"
// in include/linux/miscdevice.h
#define MISC_DYNAMIC_MINOR 255
3、The callback operation functions
As we known, there are a lot of function interface in linux (open, close...)
In our device driver, we want to operate the device like the files too, so we must rebuild the functions.
static const struct file_operations misc_fops = {
.owner = THIS_MODULE,
.read = word_count_read,
.write = word_count_write,
};
//
static ssize_t word_count_read(struct file *filp, char __user *buf,
size_t count, loff_t *ppos)
...
//
static ssize_t word_count_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos) ...
Summary :
When we build a device driver, I think we can follow the steps we analysis this driver.
1.Create the entrance function.
2.Register/Deregister the device in the entrance/exit function.
3.Build the real callback operation functions.
ok6410 android driver(2)的更多相关文章
- ok6410 android driver(5)
Test the android driver by JNI (Java Native Interface), In the third article, we know how to compile ...
- ok6410 android driver(11)
This essay, I go to a deeply studying to android HAL device driver program. According to the android ...
- ok6410 android driver(9)
In this essay, I will write the JNI to test our leds device. If you don't know how to create a jni p ...
- ok6410 android driver(8)
In the past, we know how to create and run a simple character device driver on pc, goldfish and ok64 ...
- ok6410 android driver(3)
This article discusses the Makefile and how to port the module to different platform (localhost and ...
- ok6410 android driver(12)
In this essay, I will talk about how to write the service libraries. TIPS : I won't discuss the name ...
- ok6410 android driver(10)
From this essay, we go to a new discussion "Android Hardware Abstraction Layer". In this e ...
- ok6410 android driver(7)
This article talk about how to test device driver on JNI. There are two ways to test the device driv ...
- ok6410 android driver(6)
This is a short essay about the mistakes in compiling ok6410 android-2.3 source codes. If there is n ...
- ok6410 android driver(1)
target system : Android (OK6410) host system : Debian Wheezy AMD64 1.Set up android system in ok6410 ...
随机推荐
- HDU1301&&POJ1251 Jungle Roads 2017-04-12 23:27 40人阅读 评论(0) 收藏
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25993 Accepted: 12181 De ...
- Objective-C 学习笔记(三) Numbers/数字
Objective-C Numbers/数字 Objective-C中提供了一系列的NSNumber和重要的工作方法: + (NSNumber *)numberWithBool:(BOOL)value ...
- 如何彻底删除TFS的工作项字段
TFS的工作项字段可以在所有工作项类型之间共享.例如自定义了一个字段"验证迭代"(Mycompany.IterationValidation)那么在需求.Bug中都可以添加这个字段 ...
- Replication--备份初始化需要还原备份么?
测试场景:发布服务器:SQLVM6\SQL2订阅服务器:SQLVM5\SQL2分发服务器:SQLVM3\SQL2发布数据库:RepDB2订阅数据库:RepDB2发布:RepDB2_TB1 测试步骤:1 ...
- Solr 从文件创建索引
http://blog.csdn.net/clj198606061111/article/details/21492457 http://wiki.apache.org/solr/Extracting ...
- 手动编译安装LAMP之httpd
安装前准备: 开发环境:Development Libraries 和 Development Tools httpd环境包:apr-1.4.6.tar.bz2 和 apr-util-1.4.1.ta ...
- c# 利用t4模板,自动生成Model类
我们在用ORM(比如dapper)的时候,很多时候都需要自己写Model层(当然也有很多orm框架自带了这种功能,比如ef),特别是表里字段比较多的时候,一个Model要写半天,而且Model如果用于 ...
- 原生 javaScript 百叶窗 效果的实现及原理介绍
百叶窗大家都见过吧!如图: 原理: 如图所示,空心格子就好比是每个li,给它设定相对定位属性,设置overflow:hidden: 黑块为li子元素,高度为li的2倍,设置absolute属性,我们正 ...
- Spark Streaming初步使用以及工作原理详解
在大数据的各种框架中,hadoop无疑是大数据的主流,但是随着电商企业的发展,hadoop只适用于一些离线数据的处理,无法应对一些实时数据的处理分析,我们需要一些实时计算框架来分析数据.因此出现了很多 ...
- Mybatis 优化:
Mybatis 的优化: ** 第一个 对于数据库配置的优化: 创建一个 DB.properties 的文件 里面编写Key = value 形式的数据库信息 比如: driver = com.mys ...