|
我是linux系统初学者,想写一个driver,程序如下:
#ifndef __KERNERL__
#define __KERNERL__
#endif
#ifndef MODULE
#define MODULE
#endif
#define __NO_VERSION__
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/utsname.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/config.h>
#include <linux/poll.h>
#include <linux/errno.h>
#include <linux/bitops.h>
#include <linux/wrapper.h>
MODULE_LICENSE("GPL");
#ifdef CONFIG_SMP
#define CONFIG_SMP
#endif
int init_module(void);
void cleanup_module(void);
static int asdf_open(struct inode *inode,struct file *filp);
static int asdf_release(struct inode *inode,struct file *filp);
static ssize_t asdf_read(struct file *filp,char *buf,size_t count, loff_t *fpos);
static ssize_t asdf_write(struct file* filp,const char* buf,size_t count,loff_t *f_pos);
static loff_t asdf_seek(struct file *filp,loff_t offset,int orig);
struct file_operations asdf_fops={
.llseek=asdf_seek,
.read=asdf_read,
.write=asdf_write,
.open=asdf_open,
.release=asdf_release,
};
#define DEV_NAME "testdev"
static int asdf_major;
static unsigned char asdf_body[4096]="asdf_body\n";
int init_module(void)
{
printk("<1> A simple device driver file\n");
asdf_major=register_chrdev(0,DEV_NAME,&asdf_fops);
if(asdf_major<0)
{
printk("load driver failed");
return asdf_major;
}
printk("the major is %d",asdf_major);
return 0;
}
void cleanup_module(void)
{
int ret=unregister_chrdev(asdf_major,DEV_NAME);
if(ret <0)
printk("<1>Error unregister device");
else
printk("<1>A Simple device driver removed ");
}
static int asdf_open(struct inode *inode,struct file *filp)
{
printk("open");
return 0;
}
static int asdf_release(struct inode *inode,struct file *filp)
{
printk("released");
return 0;
}
static ssize_t asdf_read(struct file *filp,char *buf,size_t count,loff_t *f_pos)
{
/* loff_t pos;
pos=*f_pos;
if((pos==4096)||(count>4096)) return 0;
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;
return count;
*/ return 0;
}
static ssize_t asdf_write(struct file *filp,const char *buf,size_t count,loff_t *f_pos)
{
/* loff_t pos;
pos=*f_pos;
if((pos==4096)||(count>4096)) return 0;
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;
return count;
*/ return 0;
}
static loff_t asdf_seek(struct file *filp,loff_t offset,int orig)
{
loff_t pos;
/*
pos=filp->f_pos;
switch(orig)
{
case 0:
pos=offset;
break;
case 1:
pos+=offset;
break;
case 2:
pos=4096+offset;
break;
default:
return -EINVAL;
}
if((pos>4096)||(pos<0)) printk("lseek err %d",pos);
return -EINVAL;
return filp->f_pos=pos;
*/
return pos;
}
MakeFile如下:
CC=gcc
MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX
asdf.o: asdf.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c asdf.c
编译产生如下结果:
gcc -Wall -DMODULE -D__KERNEL__ -DLINUX -c asdf.c
In file included from /usr/include/linux/fs.h:23,
from asdf.c:14:
/usr/include/linux/string.h:8:2: warning: #warning Using kernel header in userland!
In file included from /usr/include/linux/sched.h:14,
from asdf.c:15:
/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 asdf.c:15:
/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 asdf.c:15:
/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 asdf.c:15:
/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: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
asdf.c: At top level:
asdf.c:28: warning: `struct file' declared inside parameter list
asdf.c:28: warning: its scope is only this definition or declaration, which is probably not what you want
asdf.c:28: warning: `struct inode' declared inside parameter list
asdf.c:29: warning: `struct file' declared inside parameter list
asdf.c:29: warning: `struct inode' declared inside parameter list
asdf.c:30: warning: `struct file' declared inside parameter list
asdf.c:31: warning: `struct file' declared inside parameter list
asdf.c:32: warning: `struct file' declared inside parameter list
asdf.c:34: variable `asdf_fops' has initializer but incomplete type
asdf.c:35: unknown field `llseek' specified in initializer
asdf.c:35: warning: excess elements in struct initializer
asdf.c:35: warning: (near initialization for `asdf_fops')
asdf.c:36: unknown field `read' specified in initializer
asdf.c:36: warning: excess elements in struct initializer
asdf.c:36: warning: (near initialization for `asdf_fops')
asdf.c:37: unknown field `write' specified in initializer
asdf.c:37: warning: excess elements in struct initializer
asdf.c:37: warning: (near initialization for `asdf_fops')
asdf.c:38: unknown field `open' specified in initializer
asdf.c:38: warning: excess elements in struct initializer
asdf.c:38: warning: (near initialization for `asdf_fops')
asdf.c:39: unknown field `release' specified in initializer
asdf.c:39: warning: excess elements in struct initializer
asdf.c:39: warning: (near initialization for `asdf_fops')
asdf.c: In function `init_module':
asdf.c:47: warning: implicit declaration of function `printk'
asdf.c:48: warning: implicit declaration of function `register_chrdev'
asdf.c: In function `cleanup_module':
asdf.c:59: warning: implicit declaration of function `unregister_chrdev'
asdf.c: At top level:
asdf.c:66: warning: `struct file' declared inside parameter list
asdf.c:66: warning: `struct inode' declared inside parameter list
asdf.c:67: conflicting types for `asdf_open'
asdf.c:28: previous declaration of `asdf_open'
asdf.c:71: warning: `struct file' declared inside parameter list
asdf.c:71: warning: `struct inode' declared inside parameter list
asdf.c:72: conflicting types for `asdf_release'
asdf.c:29: previous declaration of `asdf_release'
asdf.c:76: warning: `struct file' declared inside parameter list
asdf.c:77: conflicting types for `asdf_read'
asdf.c:30: previous declaration of `asdf_read'
asdf.c:93: warning: `struct file' declared inside parameter list
asdf.c:94: conflicting types for `asdf_write'
asdf.c:31: previous declaration of `asdf_write'
asdf.c:109: warning: `struct file' declared inside parameter list
asdf.c:110: conflicting types for `asdf_seek'
asdf.c:32: previous declaration of `asdf_seek'
asdf.c:34: storage size of `asdf_fops' isn't known
asdf.c:44: warning: `asdf_body' defined but not used
make: *** [asdf.o] Error 1
请问主要问题在哪里呢,请大家指教! |
|