日期:2014-05-16  浏览次数:20563 次

第一人入门字符设备驱动程序的问题
在Linux高级开发技术里面,第二章学了一点内核驱动,虽然里面排版巨烂,但勉强能很懂
按照它的程序,自己敲到机子上之后,编译总是能不过,提示
struct   file_opterations有错不知道为啥,是不是结构体有问题???

请大虾看一下源码:

#include   <linux/types.h>
#include   <linux/fs.h>
#include   <linux/errno.h>

#define   DEFAULT   0


unsigned   int   dev_major   =   0;

static   int   dev_read(struct   inode   *node,struct   file*file,char*   buf,int   i)
{
int   a,b;
char*   c   =   "hello ";
int   j;
if(verify_area(VERIFY_WRITE,buf,i)   ==   DEFAULT)
return   DEFAULT;
/* get_user(a,int(buf[0]));
get_user(b,int(buf[1]));
a   =   a+b;
*/
for(j   =   0;j <i;j++)
{
buf[j]   =   c[j];
}
return   i;
}

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

static   int   dev_open(struct   inode   *node,struct   file*file)
{
/*MOD_INC_USE_COUNT;*/
return   0;
}

static   void   dev_release(struct   inode   *node,struct   file*file)
{
/*MOD_DEC_USE_COUNT;*/
}

struct   file_operations   dev_fops   =   {
NULL,dev_read,dev_write,NULL,
NULL,NULL,NULL,dev_open,
dev_release,NULL,NULL,NULL,NULL
};

int   init_module()
{
int   result;
result   =   register_chrdev(0, "test ",&dev_fops);
if(result   <   0)
{
printk( "Wrong! ");
return   result;
}
if(dev_major   ==   0)
dev_major   =   result;
return   0;
}

void   cleanup_module()
{
unregister_chrdev(dev_major, "mydev ");
}


应用程序的main函数:
#include   <stdio.h>
#include   <sys/types.h>
#include   <sys/stat.h>
#include   <fcntl.h>
main()
{
int   ret;
int   buf[10];
ret   =   open( "/dev/mydev ",O_RDWR);
if(ret   ==   -1)
{
printf( "cann 't   open   mydev ");
exit(0);
}
/*printf( "please   input   two   number! ");
scanf( "%d ",buf);
scanf( "%d ",buf+1);*/
read(ret,buf,1);
printf( "the   result   is:%s ",buf);
close(ret);
}


帮忙看下,有什么问题
还有呀,就是内核函数怎么查询???
用man   put_user说查不到此函数,用man   2   put_user也查不到
不知道有什么手册没???

谢了

------解决方案--------------------
struct file_operations dev_fops = {
NULL,dev_read,dev_write,NULL,
NULL,NULL,NULL,dev_open,
dev_release,NULL,NULL,NULL,NULL
};
改成下面这个试试

struct file_operations dev_fops ={
read: dev_read,
write: dev_write,
open: dev_open,
release:dev_release
};

------解决方案--------------------
内核函数怎么查询???
建议楼主装一下SOURCE INSIGHT,然后建个工程,直接把LINUX内核代码添加进去~~~
以后直接查找就可以了~~~