- 关 键 词:
- linux内核
linux中的大部分驱动程序,是以模块的形式编写的.这些驱动程序源码可以修改到内核中,也可以把他们编译成模块形势,在需要的时候动态加载.
一个典型的驱动程序,大体上可以分为这么几个部分:
1,注册设备
在系统初启,或者模块加载时候,必须将设备登记到相应的设备数组,并返回设备的主驱动号,例如:对快设备来说调用 refister_blkdec()将设备添加到数组blkdev中.并且获得该设备号.并利用这些设备号对此数组进行索引.对于字符驱动设备来说,要使用 module_register_chrdev()来获得祝设备的驱动号.然后对这个设备的所有调用都用这个设备号来实现
2,定义功能函数
对于每一个驱动函数来说.都有一些和此设备密切相关的功能函数.那最常用的块设备或者字符设备来说.都存在着诸如 open() read() write() ioctrol()这一类的操作.当系统社用这些调用时.将自动的使用驱动函数中特定的模块.来实现具体的操作.而对于特定的设备.上面的系统调用对应的函数是一定的.
如:在块驱动设备中.当系统试图读取这个设备(即调用read()时),就会运行驱动程序中的block_read() 这个函数.
打开新设备时会调用这个设备驱动程序的device_open() 这个函数.
3,谢载模块
在不用这个设备时,可以将他卸载.主要是从/proc 中取消这个设备的特殊文件.可用特定的函数实现.
下面我们列举一个字符设备驱动程序的框架.来说明这个过程.
/* 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;
}本文:http://www.qqread.com/linux/2006/05/e744105061.html
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- Linux集群技术 (8259篇文章)
- 体验Linux的音影世界 (7931篇文章)
- Linux驱动大全 (8729篇文章)
- Linux下的路由的配置与应用 (11721篇文章)
- Linux命令简介 (9775篇文章)
- Linux防火墙 (9606篇文章)
- Linux日志专题 (8383篇文章)
- Linux服务器的安全性能 (20291篇文章)
- 全面解析 Linux2.6内核 (603篇文章)
- 揭秘Linux内存管理 (7995篇文章)
- Linux与Windows Vista的桌面之争 (83次浏览)
- 对比评测Vista XP Linux启动时间 (59次浏览)
- Linux 基本概念及常用命令 (57次浏览)
- Linux系统如何判断CPU是双核还是单核 (53次浏览)
- VMware下Linux与真实主机共享上网 (31次浏览)
- Linux系统下如何更改终端显示分辨率 (30次浏览)
- 实用技巧:配置Linux操作系统环境变量 (30次浏览)
- Fedora 8 Linux下安装配置audacious过程 (27次浏览)
- Linux系统下设置环境变量Path的方法 (25次浏览)
- 最流行的发行版:Ubuntu 8.04 Alpha 2发布 (24次浏览)



