文档介绍:主要内容
(1)驱动程序概念
(2)驱动程序组织结构
(3)设备注册
(4)可装载内核模块驱动程序的实现
(5)驱动模块加载及测验 一:驱动程序概念
设备驱动程序是控制与管理硬件数据收发的软件,它是应用程序与硬件设备 沟通的桥梁n4
disk
rani
tty
tty4
ttyS12
vcs
drl
ranlD
ttyO
tty4D
ttyS13
vcsl
ecryptfs
ranll
ttyl
tty41
ttyS14
vcs2
fbO
ranl2
ttylO
tty42
ttySlS
vcs3
fd
ranl3
ttyll
tty43
ttyS16
vcs4
fgj
ranl4
ttyl2
tty 44
ttyS17
VCS5
图5
通过 mknod 命令实现
mknod /dev/<dev_name><type><major_num><minor_num>
mknod 命令
注:vdev_name>参数是特殊文件的文件名 vtype>参数,字符设备为c,块设备为b vmajor_num> 主码:定义驱动程序的类别 如:软盘的主码 2IDE 硬盘的主码为 3 四:可装载内核模块驱动程序实现 驱动程序接口声明(建立 struct file_operation 结构的实例)
struct file_operations simp'Le_fops = {
.owner =
* read =
*write = *open =
.release =
THIS_MODULE, simple_read, simple_write, simple_open, simple_release,
};
图6
字符设备的读写:
从应用程序看来,字符设备只是一个设备文件,应用程序可以像操作普通文件一 样来对设备进行操作。
file_operation 中的读写接口:
stppi* me r11pF char li^sf ^bur, <&untj WTr_? *r posj
if (c opy^tcj jtstr (buTr demgBu rftr f count ])
CQUFiit^- EFAULT;
rctu『n 匚aunt;
图7 涉及的两个函数: 由于内核态和用户态使用不同的内存定义,所以不能直接相互访问,需使用内核 交互函数。
copy_to_user 函数
extern tr,Une lon9 copy_to_user(void _user *to, const void *from, long n)
图8
copy_from_user 函数
extern inline long copy from_user( *to, const void __user *from, long n)
图9
文件的打开与释放
int simple_open(struct inode *tnode, struct file *filp)
if(stnpLe_inc>0)return -ERESTARTSYS;
simple_inc++;
return 0;
图 10
int simple