flyhappy007 发表于 2005-3-1 09:55:34

我写的linux驱动程序,有问题,帮忙指导.谢谢

.我参照网上的资料,写了第一个linux驱动程序,但是调试不能够通过,请高手给看看吧.下面是源程序,然后就是提示信息.非常感谢.我的联系方式:[email protected]
#include<linux/config.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/version.h>
#include<linux/types.h>
#include<linux/fs.h>
#include<linux/mm.h>
#include<linux/errno.h>
#include<asm/segment.h>
char kernel_version[]=UTS_RELEASE;

struct file_operations
{
        int (*seek) (struct inode *,struct file * ,off_t,int);
        int (*read) (struct inode *,struct file * ,char ,int);
        int (*write) (struct inode *,struct file * ,off_t,int);
        int (*readdir) (struct inode *,struct file *,struct dirent * ,int);
        int (*select) (struct inode *,structfile *,int ,select_table *);
        int (*ioctl) (struct inode *,struct file *,unsigned int ,unsigned long);
        int (*mmap) (struct inode *,struct file * ,struct file *,struct vm_area_struct * );
        int (*open) (struct inode *,struct file *);
        int (*release) (struct inode * ,struct file *);
        int (*fsync ) (struct inode * ,struct file *);
        int (*fasync) (struct inode *,struct file *,int);
        int (*check_media_change) (struct inode * ,struct file *);
        int (*revalidate) (dev_t dev);
};
unsigned int test_major=0;
static int read_test(struct inode *node, struct file * file,char *buf,int count)
{
        int left;
        if(verify_area(VERIFY_WRITE,buf,count)==-EFAULT)
        return -EFAULT;
        for(left=count;left>0;left--)
        {
                _put_user(1,buf,1);
                buf++;
        }
        return count;
};

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

static int open_test(struct inode * inode,struct file * file)
{
        MOD_INC_USE_COUNT;
        return 0;
};
static void release_test(struct inode *inode,struct file * file)
{
        MOD_DEC_USE_COUNT;
};
struct file_operations test_fops=
{
        NULL,
        read_test,
        write_test,
        NULL,/*test_readdir */
        NULL,
        NULL, /*test_ioctl */
        NULL, /*test_mmap */
        open_test,
        release_test,
        NULL, /*test_fsync */
        NULL, /* test_fasync */
        /*nothing more ,fill with NULLs */
};
/* 初始化设备驱动程序 */
int init_module(void)
{
        int result ;
        result=register_chrdev(0,"test",&test_fops);
        if(result<0)
        {
                printk("<0> test :can't get major number.\n");
                return result;
        }
        if(test_major==0) test_major=result;/*dynamic */
        return 0;
}
/*卸载设备驱动程序 */
void cleanup_module(void)
{
        unregister_chrdev(test_major,"test");
}

使用如下命令编译(我用的是红帽9):
# gcc -O2 -DMODULE -D_KERNEL_ -c test.c
问题提示:

In file included from /usr/include/linux/sched.h:14,
               from /usr/include/linux/mm.h:4,
               from test.c:10:
/usr/include/linux/timex.h:173: field `time' has incomplete type
In file included from /usr/include/linux/bitops.h:69,
               from /usr/include/asm/system.h:7,
               from /usr/include/linux/sched.h:16,
               from /usr/include/linux/mm.h:4,
               from test.c:10:
/usr/include/asm/bitops.h:327:2: warning: #warning This includefile is not available on all architectures.
/usr/include/asm/bitops.h:328:2: warning: #warning Using kernel headers in userspace: atomicity not guaranteed
In file included from /usr/include/linux/signal.h:4,
               from /usr/include/linux/sched.h:25,
               from /usr/include/linux/mm.h:4,
               from test.c:10:
/usr/include/asm/signal.h:107: parse error before "sigset_t"
/usr/include/asm/signal.h:110: parse error before '}' token
In file included from /usr/include/linux/sched.h:81,
               from /usr/include/linux/mm.h:4,
               from test.c:10:
/usr/include/linux/timer.h:32: field `vec' has incomplete type
/usr/include/linux/timer.h:37: field `vec' has incomplete type
/usr/include/linux/timer.h:45: parse error before "spinlock_t"
/usr/include/linux/timer.h:53: parse error before '}' token
/usr/include/linux/timer.h:63: field `list' has incomplete type
/usr/include/linux/timer.h:67: parse error before "tvec_base_t"
/usr/include/linux/timer.h:101: parse error before "tvec_bases"
/usr/include/linux/timer.h: In function `init_timer':
/usr/include/linux/timer.h:105: dereferencing pointer to incomplete type
/usr/include/linux/timer.h:105: dereferencing pointer to incomplete type
/usr/include/linux/timer.h:106: dereferencing pointer to incomplete type
/usr/include/linux/timer.h: In function `timer_pending':
/usr/include/linux/timer.h:121: dereferencing pointer to incomplete type
test.c: At top level:
test.c:17: warning: `struct file' declared inside parameter list
test.c:17: warning: its scope is only this definition or declaration, which is probably not what you want
test.c:17: warning: `struct inode' declared inside parameter list
test.c:18: warning: `struct file' declared inside parameter list
test.c:18: warning: `struct inode' declared inside parameter list
test.c:19: warning: `struct file' declared inside parameter list
test.c:19: warning: `struct inode' declared inside parameter list
test.c:20: warning: `struct dirent' declared inside parameter list
test.c:20: warning: `struct file' declared inside parameter list
test.c:20: warning: `struct inode' declared inside parameter list
test.c:21: parse error before "select_table"
test.c:21: warning: `struct file' declared inside parameter list
test.c:21: warning: `struct inode' declared inside parameter list
test.c:22: warning: `struct file' declared inside parameter list
test.c:22: warning: `struct inode' declared inside parameter list
test.c:23: warning: `struct vm_area_struct' declared inside parameter list
test.c:23: warning: `struct file' declared inside parameter list
test.c:23: warning: `struct inode' declared inside parameter list
test.c:24: warning: `struct file' declared inside parameter list
test.c:24: warning: `struct inode' declared inside parameter list
test.c:25: warning: `struct file' declared inside parameter list
test.c:25: warning: `struct inode' declared inside parameter list
test.c:26: warning: `struct file' declared inside parameter list
test.c:26: warning: `struct inode' declared inside parameter list
test.c:27: warning: `struct file' declared inside parameter list
test.c:27: warning: `struct inode' declared inside parameter list
test.c:28: warning: `struct file' declared inside parameter list
test.c:28: warning: `struct inode' declared inside parameter list
test.c:32: warning: `struct file' declared inside parameter list
test.c:32: warning: `struct inode' declared inside parameter list
test.c: In function `read_test':
test.c:35: `VERIFY_WRITE' undeclared (first use in this function)
test.c:35: (Each undeclared identifier is reported only once
test.c:35: for each function it appears in.)
test.c: At top level:
test.c:45: warning: `struct file' declared inside parameter list
test.c:45: warning: `struct inode' declared inside parameter list
test.c:50: warning: `struct file' declared inside parameter list
test.c:50: warning: `struct inode' declared inside parameter list
test.c: In function `open_test':
test.c:52: union has no member named `usecount'
test.c: At top level:
test.c:55: warning: `struct file' declared inside parameter list
test.c:55: warning: `struct inode' declared inside parameter list
test.c: In function `release_test':
test.c:57: union has no member named `usecount'
test.c: At top level:
test.c:62: warning: initialization from incompatible pointer type
test.c:63: warning: initialization from incompatible pointer type
test.c:68: warning: initialization from incompatible pointer type
test.c:69: warning: initialization from incompatible pointer type
test.c:91:2: warning: no newline at end of file
谢谢.非常感谢.

zxyjoseph 发表于 2005-3-3 09:33:26

很不错啊,我毕业设计是设计驱动程序,我们能不能联系联系啊!!!!!

xinhe007 发表于 2005-3-19 22:00:35

我的毕业设计也是写驱动的

foxship305 发表于 2005-3-25 17:36:25

我毕业设计不是写驱动。是研究源代码。现在已经毕业半年了。

lxb685 发表于 2005-3-28 21:02:15

小伙子,在编译的时候用__KERNEL__ (两个杠)看看,而不是_KERNEL_,

flyhappy007 发表于 2005-3-31 09:04:59

#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
应该在文件的开头添加如上代码就可以了。lxb685说的对。谢谢。

mwq 发表于 2005-4-1 15:01:06

我的毕业设计也是开发驱动和应用程序,但我的是嵌入式的开发.

WindTrace 发表于 2005-4-3 01:51:52

struct file_operations test_fops=
{
NULL,
read_test,
write_test,
NULL, /*test_readdir */
NULL,
NULL, /*test_ioctl */
NULL, /*test_mmap */
open_test,
release_test,
NULL, /*test_fsync */
NULL, /* test_fasync */
/*nothing more ,fill with NULLs */
};
这一段应该写成这样的格式才不容易错
struct file_operations test_fops=
{
.........
read:    read_test,
write:    write_test,
.........
};

还有,现在比较流行这样写
module_init(MyModule_init);               ----》括号里面的就是初始化函数
module_exit(MyModule_exit);                  》推出时候的函数
同时要加上#include <linux/init.h>载在开头的时候
其实这两个东西就是一个宏来的,相当于调用了
init_module(void)以及 cleanup_module(void)
所以这两个函数在上面的程序中必须改名,不然就会错误~~~

Enjoy~~~~

kukoo 发表于 2005-4-7 12:16:33

编译选项不对
代码的file_opertions定义是怎么回事,重定义了,你什么版本的内核啊?

swoderheart 发表于 2005-4-25 10:52:20

?

我也是参考网上的编写驱程的资料后写这个程序,也是用的RH9,用的是2.4.20-8的内核版本,编译后出现跟你一样的问题。
        #ifndef __KERNEL__
        #define __KERNEL__
        #endif
        #ifndef MODULE
        #define MODULE
        #endif
        另外你说在文件头上加上上面几行代码就可以了,可是为什么我的还是不行的。不知道你最后是怎么修改和编译的,能告诉我吗,谢谢,麻烦你了 :)

xue_hu2001 发表于 2005-4-26 15:14:53

WindTrace 用的是2.6的内核写法!!

youyu8486 发表于 2005-5-10 22:16:28

我想问一下大家,有没有人知道那个驱动程序中的verify_area以及_put_user()是做什么的?_put_user()中的三个变量究竟指什么?

sd_2001 发表于 2005-5-12 17:38:58

编译选项不对吧,include 路径应该是Linux内核目录下的include 而不应该使用默认的应用程序include目录吧。

gcc -O2 -DMODULE -D__KERNEL -I/usr/src/linux/include -c test.c

这样试试

greenivy 发表于 2005-5-20 09:16:19

斑竹,我也做得Linux下的一个串口驱动,用的是redhat9.0,实在用户态下用GCC编译的,但总是出现“找不到头文件”的错误,请问是怎么回事啊,是不是不能在用户态下做啊??

blue_hacker 发表于 2005-6-1 16:34:45

所有 这种错误基本上就 两个原因:
1.没有定义__KERNEL__ __MODULE__
2.没有指定正确的内核头文件包含路径
页: [1] 2
查看完整版本: 我写的linux驱动程序,有问题,帮忙指导.谢谢