内核版本:2.4.22
阅读此文的目的:学会编撰Linux设备驱动。
阅读此文的方式:阅读以下2个文件:hello.c,asdf.c。
此文假定读者:
早已能用C语言编撰Linux应用程序,
理解"字符设备文件,块设备文件,主设备号linux设备驱动开发详解ldd,次设备号",
会写简单的Shell脚本和Makefile。
1."hello.c"
--------------------------------
/*
*这是我们的第一个源文件,
*它是一个可以加载的内核模块redhat linux下载,
*加载时显示"Hello,World!",
*卸载时显示"Bye!"。
*须要说明一点,写内核或内核模块不能用写应用程序时的系统调用或函数库,
*由于我们写的就是为应用程序提供系统调用的代码。
*内核有专用的函数库,如linux/kernel.h>,linux/fs.h>,linux/sche.h>等,
*现今还没必要了解得很详尽,
*这儿用到的printk的功能类似于printf。
*"/usr/src/linux"是你实际的内核源码目录的一个符号链接,
*若果没有如今就创建一个,由于下边和之后还会用到。
*编译它用"gcc-c-I/usr/src/linux/includehello.c",
*假如正常会生成文件hello.o,
*加载它用"insmodhello.o",
*只有在文本终端下能够看见输出。
*卸载它用"rmmodhello"
*/
/*
*小方法:在用户目录的.bashrc里加上一行:
*aliasmkmod=''gcc-c-I/usr/src/linux/include''
*之后重新登录Shell,
*之后就可以用"mkmodhello.c"的形式来编译内核模块了。
*/
/*开始例行公事*/
#ifndef__KERNEL__
#define__KERNEL__
#endif
#ifndefMODULE
#defineMODULE
#endif
#includelinux/config.h>
#includelinux/module.h>
MODULE_LICENSE("GPL");
#ifdefCONFIG_SMP
#define__SMP__
#endif
/*结束例行公事*/
#includelinux/kernel.h>/*printk()在这个文件里*/
staticint
init_module
(){
printk("Hello,World!n");
return0;/*假如初始工作失败,就返回非0*/
staticvoid
cleanup_module
(){
printk("Bye!n");
------------------------------------
2."asdf.c"
------------------------------------
/*
*这个文件是一个内核模块。
*内核模块的编译,加载和卸载在上面早已介绍了。
*这个模块的功能是,创建一个字符设备。
*这个设备是一块4096字节的共享显存。
*内核分配的主设备号会在加载模块时显示。
*/
/*开始例行公事*/
#ifndef__KERNEL__
#define__KERNEL__
#endif
#ifndefMODULE
#defineMODULE
#endif
#includelinux/config.h>
#includelinux/module.h>
#ifdefCONFIG_SMP
#define__SMP__
#endif
MODULE_LICENSE("GPL");
/*结束例行公事*/
#includeasm/uaccess.h>/*copy_to_user(),copy_from_user*/
#includelinux/fs.h>/*structfile_operations,register_chrdev(),...*/
#includelinux/kernel.h>/*printk()在这个文件里*/
#includelinux/sched.h>/*和任务调度有关*/
#includelinux/types.h>/*u8,u16,u32...*/
/*
*关于内核功能库,可以去网上搜索详尽资料,
*/
/*文件被操作时的反弹功能*/
staticintasdf_open(structinode*inode,structfile*filp);
staticintasdf_release(structinode*inode,structfile*filp);
staticssize_tasdf_read(structfile*filp,char*buf,size_tcount,loff_t*f_pos);
staticssize_tasdf_write(structfile*filp,constchar*buf,size_tcount,loff_t*f_pos);
staticloff_tasdf_lseek(structfile*file,loff_toffset,intorig);
/*申请主设备号时用的结构,在linux/fs.h里定义*/
structfile_operationsasdf_fops={
open:asdf_open,
release:asdf_releaselinux设备驱动开发详解ldd,
read:asdf_read,
write:asdf_write,
llseek:asdf_lseek,
};
staticintasdf_major;/*拿来保存申请到的主设备号*/
staticu8asdf_body[4096]="asdf_bodyn";/*设备*/
staticint
init_module
(){
printk("Hi,This''ASimpleDeviceFile!n");
asdf_major=register_chrdev(0,"ASimpleDeviceFile",asdf_fops);/*申请字符设备的主设备号*/
if(asdf_major0)returnasdf_major;/*申请失败就直接返回错误编号*/
printk("Themajoris:%dn",asdf_major);/*显示申请到的主设备号*/
return0;/*模块正常初始化*/
staticvoid
cleanup_module
(){
unregister_chrdev(asdf_major,"ASimpleDeviceFile");/*注销之后,设备就不存在了*/
printk("ASimpleDevicehasbeenremoved,Bye!n");
/*
*编译这个模块之后加载它,
*假如正常,会显示你的设备的主设备号。
*如今你的设备就完善好了,我们可以测试一下。
*假定你的模块申请到的主设备号是254,
*运行"mknodabcc2540",就完善了我们的设备文件abc。
*可以把它当作一个4096字节的显存块来测试一下,
*诸如"catabc","cpabcimage","cpimageabc",
*或写几个应用程序用它来进行通信。
*介绍一下两个须要注意的事,
*一是printk()的显示只有在非图形模式的终端下能够看见,
*二是加载过的模块最好在不用之后卸载掉。
*假如对Linux环境的系统调用很陌生linux查看进程,建议先看APUE这本书。
*/
staticint
asdf_open/*open反弹*/
structinode*inode,
structfile*filp
){
printk("^_^:open%sn",
current->comm);
/*
*应用程序的运行环境由内核提供,内核的运行环境由硬件提供。
*这儿的current是一个指向当前进程的表针,
*现今没必要了解current的细节。
*在这儿,当前进程正打开这个设备,
*返回0表示打开成功,内核会给它一个文件描述符。
*这儿的comm是当前进程在Shell下的command字符串。
*/
return0;
staticint
asdf_release/*close反弹*/
structinode*inode,
structfile*filp
){
printk("^_^:closen");
return0;
staticssize_t
asdf_read/*read反弹*/
structfile*filp,
char*buf,
size_tcount,
loff_t*f_pos
){
loff_tpos;
pos=*f_pos;/*文件的读写位置*/
if((pos==4096)||(count>4096))return0;/*判定是否早已到设备尾,或写的宽度超过设备大小*/
pos=count;
if(pos>4096){
count-=(pos-4096);
pos=4096;
if(copy_to_user(buf,asdf_body*f_pos,count))return-EFAULT;/*把数据讲到应用程序空间*/
*f_pos=pos;/*改变文件的读写位置*/
returncount;/*返回读到的字节数*/
staticssize_t
asdf_write/*write反弹,和read一一对应*/
structfile*filp,
constchar*buf,
size_tcount,
loff_t*f_pos
){
loff_tpos;
pos=*f_pos;
if((pos==4096)||(count>4096))return0;
pos=count;
if(pos>4096){
count-=(pos-4096);
pos=4096;
if(copy_from_user(asdf_body*f_pos,buf,count))return-EFAULT;
*f_pos=pos;
returncount;
staticloff_t
asdf_lseek/*lseek反弹*/
structfile*file,
loff_toffset,
intorig
){
loff_tpos;
pos=file->f_pos;
switch(orig){
case0:
pos=offset;
break;
case1:
pos=offset;
break;
case2:
pos=4096offset;
break;
default:
return-EINVAL;
if((pos>4096)||(pos0)){
printk("^_^:lseekerror%dn",pos);
return-EINVAL;
returnfile->f_pos=pos;