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 ...
随机推荐
- java并发编程实战:第十三章----显示锁
一.Lock与ReentrantLock Lock接口中定义了一种无条件.可轮询的.定时的以及可中断的锁获取操作,所有加锁和解锁的方法都是显式的. 1 public interfece Lock 2 ...
- ETL开发
要进入开发阶段,了解不同的ETL产品. 整个ETL系统中,时间或更精确的,吞吐量是主要关心的内容.这种转换处理任务设计的主要目的归根结底是使得数据装载到展现表中最快并使得最终用户能快速的从这些表中得到 ...
- POJ 3685 Matrix (二分套二分)
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8674 Accepted: 2634 Descriptio ...
- handsontable-developer guide-data binding,data sources
数据绑定: 1.表格中得数据是引用了数据源中的数据:表格中数据改变,数据源中得数据也改变:数据源中得数据改变,通过render方法,表格中的数据也改变: 2.如果想把数据源中的数据和表格中的数据分开: ...
- An error "Host key verification failed" when you connect to other computer by OSX SSH
Here's quick way to remove all entries in the host file: In an OSX terminal, type rm -f ~/.ssh/known ...
- IPA文件的自动化生成和无线分发
1. IPA的无线分发 iOS应用开发测试过程中,通过无线网络进行IPA包的分发将是非常便捷的,于是也就有了类似testflightapp之类的平台.对于这一功能,我们也可以自己实现,只需要一个简单的 ...
- Buffer Pool--内存相关术语
虚拟地址空间(virtual address space): 供应用程序能够申请访问的最大地址空间,32位系统上为4GB,64位系统上是8TB,虚拟地址空间映射的数据不一定存放在物理内存中,还可能存放 ...
- C# 实用小类
/// <summary> /// 汉字转换拼音 /// </summary> /// <param name="PinYin"></pa ...
- .NET Framework 历史版本(2017年)
.NET简介 这个平台相信我们都知道,不过随着技术发展,现在的.NET平台也今非昔比. .NET平台类似Java平台,是微软于2000年推出的Windows操作系统的应用软件开发框架,发展至今形成巨大 ...
- .net core执行dotnet ef migrations createmodel等命令出错
.net core执行dotnet ef migrations createmodel等命令出错 执行dotnet ef migrations createmodel.dotnet ef migrat ...