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 ...
随机推荐
- Low-level Thinking in High-level Shading Languages
因为要反汇编shader代码,所以google了数学函数_sat的知识,发现了一些高级着色语言的优化相关的问题.Low-level Thinking in High-level Shading Lan ...
- 升级windows 10后网络连接异常
升级 windows 10,QQ无法连接,显示“登陆超时,请检查网络或者防火墙设置”.打开360软件助手,准备升级QQ试试,360软件助手也显示网络异常. 解决方法: 右键点击开始菜单,命令提示符(管 ...
- 原生态在Hadoop上运行Java程序
第一种:原生态运行jar包1,利用eclipse编写Map-Reduce方法,一般引入Hadoop-core-1.1.2.jar.注意这里eclipse里没有安装hadoop的插件,只是引入其匝包,该 ...
- Python 定制类 特殊方法
1.特殊方法 定义在class中 不需要直接调用,python的某些函数或操作符会自动的调用对应的特殊方法. 如定义了person类,使用print p 语句打印person类的实例时,就调用了特殊方 ...
- sql-修改每条数据的某一个字段的值
update B set B.maildata =(select SUBSTRING(maildata,0,3) from basedata where basedata.cid = B.cid)+( ...
- 关于My Sql update语句不能用子查询的解决办法
在使用My Sql数据库语法操作update时,第一时间想到的是一下写法: UPDATE purchase_request_detail SET convert_to_voucher_id=, con ...
- git 使用merge 对本地分支进行合并 并进行代码提交的流程
1.只有当将修改内容commit后 该修改才完全生效,进行merge前需要将两个分支修改的内容都进行commit 2.假设本地两个分支 用于开发的分支:dev 用于同步远程仓库的分支:mas ...
- ObjectARX动态添加AutoCAD传统下拉菜单入门篇(一)
ObjectARX动态添加传统下拉菜单入门篇 图文by edata , 转载注明出处 http://www.cnblogs.com/edata AutoCAD 添加传统下拉菜单有很多种方式,比较典型 ...
- mysql 赋给用户远程权限 grant all privileges on
我配置了权限 就可以在Windows下访问我虚拟机中的数据库了 来源:http://blog.csdn.net/louisliaoxh/article/details/52767209 登录: 在本机 ...
- [CSS3] 边栏导航动画
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...