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 ...
随机推荐
- Geronimo 叛逆者: 使用集成软件包:Codehaus 的 Woodstox(转载)
XML 解析器通常是高性能.健壮应用程序的关键.传统的 XML 解析技术包括文档对象模型(Document Object Model,DOM)和 Simple API for XML (SAX).现在 ...
- 使用 log4j 打印日志
开发阶段:发现程序的问题,排错 产品阶段:记录程序运行的状况 Maven中配置依赖 通过配置文件输出日志的格式,输送的位置等 一.入门实例 1.新建一个JAva工程,导入包log4j-1.2.17.j ...
- 学习python的第五天
4.30自我总结 一复习 1.查看数据类型 #数值10的位置 print(di(10)) #数值10的样式 print(type(10)) 2.关于变量的一些补充 a=1 b=1 c=1 #a,b,c ...
- SQLServer中数据加密方法
对SQLServer中的数据进行加密,有三种方法, 1. 在程序语言中先对数据进行加密后再把加密后的数据保存在SQLServer数据库中: 2. 利用SQLServer未公开的加密密码函数,在SQ ...
- Spring AOP 简单入门笔记 (转)
分享一个自己写的最为简单的Spring AOP的应用,其实,本人也是学习Spring不久,只是把一些个人的理解分享下,供参考.可能很多人刚开始不太理解到底啥是AOP,其实它也是相对 OOP来说的,类似 ...
- Postgres 主从配置(五)
PostgreSQL 9.4 新增的一个特性, replication slot, 1. 可以被流复制的sender节点用于自动识别它xlog中的数据在下面的standby中是否还需要(例如, st ...
- [Erlang31]Erlang trace总结
在一个并行的世界里面,我们很难做到单步断点调试来定位问题(太多的消息飞来飞去),Erlang设计者也深刻体会到这一点,推出了另一个trace机制. 通过这个trace,你可以: .特定进程集内的函数调 ...
- C#发送邮件及注意事项
//参数配置 static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings[&qu ...
- SQL Server—— 如何创建定时作业
在做SQL server 管理时,往往需要每日执行定时任务,但是如果每天都去人工执行,非常不方便,而且一般定时操作,都应该是在数据库压力不大时,一般是在夜间.所以我们需要创建定时作业来代替人工的执行定 ...
- iOS 添加字体
1. 将字体(ttf 文件)导入项目. 2. 在项目plist 文件里的 Fonts provided by application 添加新导入的字体. 3. 代码中的调用 [aLabel setFo ...