我写了个驱动程序,编译没错,但是insmod的时候就出现
moduletest: please compile with -fno-common
insmod: cannot insert `moduletest.ko': Invalid module format (-1): Exec format error的错误,不知道为什么,求高手指教.
源码如下:
#include
#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif
MODULE_LICENSE("GPL");
#include
#include
#include
#include
#include
#include //for udelay
#include
#include
#define GPFCON (*(volatile unsigned long *) 0x56000050)
#define GPFDAT (*(volatile unsigned long *) 0x56000054)
#define GPF4_in ~(1<<(4*2))
unsigned long dwdat;
unsigned int fs_major = 0;
static char *data;
static dev_OPEN=0;
static ssize_t GPIO_read(struct file *file,char *buf,size_t count,loff_t *f_pos);
static ssize_t GPIO_write(struct file *file,const char *buffer,size_t count,loff_t *f_pos);
static int GPIO_open(struct inode *inode,struct file *file);
static int GPIO_release(struct inode *inode,struct file *file);
static ssize_t GPIO_ioctl(struct inode *inode,struct file *file,unsigned int ret,unsigned long data);
int init_module(void);
void cleanup_module(void);
static struct file_operations GPIO_fops = {
read: GPIO_read,
write: GPIO_write,
ioctl: GPIO_ioctl,
open: GPIO_open,
release: GPIO_release,
};
static ssize_t GPIO_read(struct file *file,char *buf,size_t count,loff_t *f_pos)
{
int len;
if(count<0)
return -EINVAL;
len = strlen(data);
if(len
count = len;
copy_to_user(buf,data,count+1);
return count;
}
static ssize_t GPIO_write(struct file *file,const char *buffer,size_t count,loff_t *f_pos)
{
if(count < 0)
return -EINVAL;
kfree(data);
data = (char *)kmalloc(sizeof(char)*(count+1),GFP_KERNEL);
if(!data)
return -ENOMEM;
copy_from_user(data,buffer,count+1);
return count;
}
static int GPIO_open(struct inode *inode,struct file *file)
{
//MOD_INC_USE_COUNT;
dev_OPEN++;
printk("This is open\n");
return 0;
}
static ssize_t GPIO_ioctl(struct inode *inode,struct file *file,unsigned int ret,unsigned long data)
{ GPFCON=GPF4_in;
dwdat=GPFDAT;
ret=dwdat&(1<<4);
if(ret) //如果是高电平返回1
{
return ret;
}
}
static int GPIO_release(struct inode *inode,struct file *file)
{
//MOD_DEC_USE_COUNT;
dev_OPEN--;
//module_put(fs_major);
printk("this is released\n");
return 0;
}
int init_module(void)
{
int res;
res=register_chrdev(0,"my2440dev",&GPIO_fops);
printk("can get major name %d!\n ",res);
if(res<0)
{
printk("can't get major name!\n");
return res;
}
if(fs_major ==0)
fs_major = res;
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(fs_major,"my2440dev");
}