日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

Linux內(nèi)核模塊和驅(qū)動(dòng)的編寫( 二 )


一個(gè)典型的驅(qū)動(dòng)程序,大體上可以分為這么幾個(gè)部分:
1.注冊設(shè)備
在系統(tǒng)初啟,或者模塊加載時(shí)候,必須將設(shè)備登記到相應(yīng)的設(shè)備數(shù)組,并返回設(shè)備的主驅(qū)動(dòng)號,例如:對快設(shè)備來說調(diào)用 refister_blkdec()將設(shè)備添加到數(shù)組blkdev中,并且獲得該設(shè)備號,并利用這些設(shè)備號對此數(shù)組進(jìn)行索引 。對于字符驅(qū)動(dòng)設(shè)備來說,要使用 module_register_chrdev()來獲得祝設(shè)備的驅(qū)動(dòng)號,然后對這個(gè)設(shè)備的所有調(diào)用都用這個(gè)設(shè)備號來實(shí)現(xiàn) 。
2.定義功能函數(shù)
對于每一個(gè)驅(qū)動(dòng)函數(shù)來說,都有一些和此設(shè)備密切相關(guān)的功能函數(shù),那最常用的塊設(shè)備或者字符設(shè)備來說,都存在著諸如 open() read() write() ioctrol()這一類的操作 。當(dāng)系統(tǒng)社用這些調(diào)用時(shí),將自動(dòng)的使用驅(qū)動(dòng)函數(shù)中特定的模塊,來實(shí)現(xiàn)具體的操作 。而對于特定的設(shè)備,上面的系統(tǒng)調(diào)用對應(yīng)的函數(shù)是一定的 。
如:在塊驅(qū)動(dòng)設(shè)備中.當(dāng)系統(tǒng)試圖讀取這個(gè)設(shè)備(即調(diào)用read()時(shí)),就會運(yùn)行驅(qū)動(dòng)程序中的block_read() 這個(gè)函數(shù) 。
打開新設(shè)備時(shí)會調(diào)用這個(gè)設(shè)備驅(qū)動(dòng)程序的device_open() 這個(gè)函數(shù).

3.謝載模塊
在不用這個(gè)設(shè)備時(shí),可以將他卸載,主要是從/proc 中取消這個(gè)設(shè)備的特殊文件,可用特定的函數(shù)實(shí)現(xiàn) 。
下面我們列舉一個(gè)字符設(shè)備驅(qū)動(dòng)程序的框架.來說明這個(gè)過程.
/* a module of a character device */
/* some include files*/
#include"param.h"
#include"user.h"
#include"tty.h"
#include"dir.h"
#include”fs.h"
/* the include files modules need*/
#include"Linux/kernel.h"
#include"Linux/module.h"
#if CONFIG_MODBERSIONS==1
degine MODBERSIONS
#include" Linux.modversions.h"
#endif
#difine devicename mydevice
/* the init funcion*/
int init_module()
{
int tag=module_register_chrdev(0,mydevice,&Fops);
if (tag<0)
{
printk("the device init is erro!n");
return 1;
}
return 0;
}
/*the funcion which the device will be used */
int device_open ()
{
…….
}
int device_read ()
{
…….
}
int device_write ()
{
…….
}
int device_ioctl ()
{
…….
}
……
/* the deltter function of this module*/
int cleanup_module()
{
int re=module_unregister_chrdev(tag,mydevice);
if( re<0)
{
printk("erro unregister the module !!n");
return 1;
}
return 0;
【Linux內(nèi)核模塊和驅(qū)動(dòng)的編寫】}


推薦閱讀