设为首页 加入收藏

TOP

学习Linux内核小记
2014-11-24 02:26:12 来源: 作者: 【 】 浏览:2
Tags:学习 Linux 内核 小记

test.c:


#define __NO_VERSION__
#include
#include


#include
#include
#include
#include
#include
unsigned int test_major = 0; //全局变量,主设备号


static int read_test(struct inode *node,struct file *file,char *buf,int count) //这个函数是为read调用准备的.当调用read时,read_test()被调用,它把用户的缓冲区全部写1
{


int left;

if (verify_area(VERIFY_WRITE,buf,count) == -EFAULT ) //验证buf是否可用
return -EFAULT;

for(left = count ; left > 0 ; left--)
{
__put_user(1,buf,1); //用于向用户传送数据,这是kernel提供的一个函数
buf++; //指针后移
}
return count; //返回buf的大小
}


static int write_tibet(struct inode *inode,struct file *file,const char *buf,int count)
{
return count;
}


static int open_tibet(struct inode *inode,struct file *file )
{
MOD_INC_USE_COUNT;
return 0;
}


static void release_tibet(struct inode *inode,struct file *file )
{
MOD_DEC_USE_COUNT;
}


struct file_operations test_fops = { //把系统调用和驱动程序关联起来。系统调用通过设备文件的主设备号找到相应的设备驱动程序,然后读取这个数据结构相应的函数指针,接着把控制权交给该函数
NULL,
read_test,
write_test,
NULL,
NULL,
NULL,
NULL,
open_test,
release_test, NULL,
NULL,
NULL,NULL
};


int init_module(void)
{
int result;

result = register_chrdev(0, "test", &test_fops); //向系统的字符设备表登记了一个字符设备,如果登记成功,返回设备的主设备号

if (result < 0) { //不成功,返回一个负值
printk(KERN_INFO "test: can't get major number\n");
return result;
}

if (test_major == 0) test_major = result;
return 0;
}


void cleanup_module(void)
{
unregister_chrdev(test_major, "test"); //把register_chrdev()中自动分配的字符设备号给注销掉
}


编译:


$ gcc -O2 -DMODULE -D__KERNEL__ -c test.c


得到文件test.o就是一个设备驱动程序


安装到系统:


$ insmod -f test.o


卸载:


$ rmmod test
创建设备文件:


$ mknod /dev/test c major minor


c 是指字符设备,major是主设备号,就是在/proc/devices里看到的。


用shell命令


$ cat /proc/devices | awk "}"


minor是从设备号,设置成0就可以了


现在可以通过设备文件来访问我们的驱动程序了。


测试文件:


#include
#include
#include
#include


main()
{
int testdev;
int i;
char buf[10];

testdev = open("/dev/test",O_RDWR); //打开设备文件

if ( testdev == -1 )
{
printf("Cann't open file \n");
exit(0);
}

read(testdev,buf,10); //调用系统调用read,把用户的缓冲区全部写1

for (i = 0; i < 10;i++)
printf("%d\n",buf[i]);

close(testdev); //关闭设备文件
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Ubuntu中用vim打造shell编程调试I.. 下一篇Linux C/C++对Popt库分析命令行的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: