2.定义功能函数
对于每一个驱动函数来说,都有一些和此设备密切相关的功能函数,那最常用的块设备或者字符设备来说,都存在着诸如open()read()write()ioctrol()这一类的操作。当系统社用这些调用时,将自动的使用驱动函数中特定的模块,来实现具体的操作。而对于特定的设备,上面的系统调用对应的函数是一定的。
如:在块驱动设备中.当系统试图读取这个设备(即调用read()时),就会运行驱动程序中的block_read()这个函数。
打开新设备时会调用这个设备驱动程序的device_open()这个函数.
3.谢载模块
在不用这个设备时,可以将他卸载,主要是从/proc中取消这个设备的特殊文件,可用特定的函数实现。
下面我们列举一个字符设备驱动程序的框架.来说明这个过程.
/*amoduleofacharacterdevice*/
/*someincludefiles*/
#include"param.h"
#include"user.h"
#include"tty.h"
#include"dir.h"
#include”fs.h"
/*theincludefilesmodulesneed*/
#include"Linux/kernel.h"
#include"Linux/module.h"
#ifCONFIG_MODBERSIONS==1
degineMODBERSIONS
#include"Linux.modversions.h"
#endif
#difinedevicenamemydevice
/*theinitfuncion*/
intinit_module()
{
inttag=module_register_chrdev(0,mydevice,&Fops);
if(tag<0)
{
printk("thedeviceinitiserro!\n");
return1;
}
return0;
}
/*thefuncionwhichthedevicewillbeused*/
intdevice_open()
{
…….
}
intdevice_read()
{
…….
}
intdevice_write()
{
…….
}
intdevice_ioctl()
{
…….
}
……
/*thedeltterfunctionofthismodule*/
intcleanup_module()
{
intre=module_unregister_chrdev(tag,mydevice);
if(re<0)
{
printk("errounregisterthemodule!!\n");
return1;
}
return0;
}
