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)的更多相关文章

  1. ok6410 android driver(5)

    Test the android driver by JNI (Java Native Interface), In the third article, we know how to compile ...

  2. ok6410 android driver(11)

    This essay, I go to a deeply studying to android HAL device driver program. According to the android ...

  3. 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 ...

  4. ok6410 android driver(8)

    In the past, we know how to create and run a simple character device driver on pc, goldfish and ok64 ...

  5. ok6410 android driver(3)

    This article discusses the Makefile and how to port the module to different platform (localhost and ...

  6. ok6410 android driver(12)

    In this essay, I will talk about how to write the service libraries. TIPS : I won't discuss the name ...

  7. ok6410 android driver(10)

    From this essay, we go to a new discussion "Android Hardware Abstraction Layer". In this e ...

  8. ok6410 android driver(7)

    This article talk about how to test device driver on JNI. There are two ways to test the device driv ...

  9. ok6410 android driver(6)

    This is a short essay about the mistakes in compiling ok6410 android-2.3 source codes. If there is n ...

  10. ok6410 android driver(1)

    target system : Android (OK6410) host system : Debian Wheezy AMD64 1.Set up android system in ok6410 ...

随机推荐

  1. windows server2012怎样关机怎样重启-详细教程

    | 浏览:1991 | 更新:2014-12-15 17:33 1 2 3 4 5 6 分步阅读 百度经验:jingyan.baidu.com windows server2012和以往有些不同,关机 ...

  2. awk基础02-变量-分隔符-数组

        对任意一门语言都会有变量,在awk中变量分为内置变量和自定义变量. 内置变量:就是预先在awk中定义好的,用户可以直接使用 自定义变量:这种变量为用户自己定义的变量,需要先定义后再使用. 内置 ...

  3. in操作符

    // Arrays,数组:下标 in array,length也可以 var trees = new Array("redwood", "bay", " ...

  4. 别做HR最讨厌的求职者

    有些求职者认为自己各方面都与所应聘的职位要求相匹配,因此在被淘汰之后总是特别不解,努力回忆起每个面试环节,却始终找不到原因.是的,也许你真的很优秀,但是你被淘汰了,原因也许并不大,只是你得罪了HR.其 ...

  5. Sharepoint安装的几处注意事项

    0.sharepoint自带组件安装,无需另下载安装 1.必须安装域(不安装会提示sharepoint 指定的用户是本地账户) 2.域安装后需要在sharepoint设置的数据库账号具有域权限及高级权 ...

  6. Buffer Pool--SQL Server:Buffer Manager 对象

    --============================================================== --参考链接:http://technet.microsoft.com ...

  7. Linux下source文件两种方法

    1.直接source命令加文件 source /etc/rc.d/init.d/functions 2.点(.)加文件 . /etc/rc.d/init.d/functions

  8. asp.net——XML格式导出Excel

    下面介绍一种导出Excel的方法: 此方法不需要在服务器上安装Excel,采用生成xml以excel方式输出到客户端,可能需要客户机安装excel,所以也不会有乱七八糟的权限设定,和莫名其妙的版本问题 ...

  9. 基于ASP.NET MVC 利用(Aspose+Pdfobject.js) 实现在线预览Word、Excel、PPT、PDF文件

    #region VS2010版本以及以上版本源码下载地址:http://download.csdn.net/download/u012949335/10231812 VS2012版本以及以上版本源码下 ...

  10. 知物由学 | AI网络安全实战:生成对抗网络

    本文由  网易云发布. “知物由学”是网易云易盾打造的一个品牌栏目,词语出自汉·王充<论衡·实知>.人,能力有高下之分,学习才知道事物的道理,而后才有智慧,不去求问就不会知道.“知物由学” ...