[问题]一个简单字符驱动程序的问题

软件和网站开发以及相关技术探讨
回复
flowerfish
帖子: 15
注册时间: 2005-10-15 9:50
来自: HUST

[问题]一个简单字符驱动程序的问题

#1

帖子 flowerfish »

下面是我编的一个字符设备驱动程序,编译是可以通过,但是想要加载驱动程序却加载不了了,请大家帮我分析下问题。

##################################
源程序:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <asm/uaccess.h>

//#define USERDRV_MAJOR 0

static int userdrv_init(void);
static int userdrv_open(struct inode *inode,struct file *file);
static int userdrv_close(struct inode *inode,struct file *file);
static ssize_t userdrv_read(struct file *file,char __user *buf,size_t count,loff_t *offset);
static ssize_t userdrv_write(struct file *file,const char __user *buf,size_t count,loff_t *offset);
static void userdrv_cleanup(void);

static int userdrv_major=0;
static int userdrv_flag=0;
static int userdrv_initialized = 0;
char message[10];

struct file_operations userdrv_fops={
.owner=THIS_MODULE,
.read=userdrv_read,
.write=userdrv_write,
.open=userdrv_open,
.release=userdrv_close
};

static int userdrv_init(void)
{
int i;
if(userdrv_initialized==1) return 0;
i=register_chrdev(0,"usr_drv",&userdrv_fops);
if(i<0){
printk(KERN_INFO "test: can't get major number\n");
return i;
}
if (userdrv_major == 0) userdrv_major=i;
printk(KERN_CRIT"USERDRV:userdrv registerred successfully:)=\nthe major is %d",i);

userdrv_initialized=1;
return 0;
}

static int userdrv_open(struct inode *inode,struct file *file)
{
if(userdrv_flag==1)
{
return -1;
}
printk(KERN_CRIT"USERDRV:userdrv device open \n");
//MOD_INC_USE_COUNT;
userdrv_flag = 1;
return 0;
}

static int userdrv_close(struct inode *inode,struct file *file)
{
if(userdrv_flag==1){
printk(KERN_CRIT "USERDRV:userdrv device close\n");
//MOD_DEC_USE_COUNT;
userdrv_flag = 0;
return 0;
}
else{
return -1;
}
}

static ssize_t userdrv_read(struct file *file,char __user *buf,size_t count,loff_t *offset)
{
copy_to_user(buf,message,count);
return count;
}

static ssize_t userdrv_write(struct file *file,const char __user *buf,size_t count,loff_t *offset)
{
copy_from_user(message,buf,count);
return count;
}

static void userdrv_cleanup(void)
{
if(userdrv_initialized==1){
unregister_chrdev(userdrv_major,"usr_drv");
userdrv_initialized=0;
printk(KERN_CRIT "USERDRV:userdrv device is cleanup\n");
}
return;
}

module_init(userdrv_init);
module_exit(userdrv_cleanup);
MODULE_LICENSE("GPL");


###############################
编译及加载过程:
root@flowerfish:/home/flowerfish/OSwork/num4 # gcc -Wall -DMODULE -D__KERNEL__ -DLINUX -I/usr/src/linux/include -c userdrv1.c -o userdrv1.o
root@flowerfish:/home/flowerfish/OSwork/num4 # insmod -f userdrv1.o
insmod: error inserting 'userdrv1.o': -1 Invalid module format
到了这一步就出问题拉,请大家帮忙分析下吧
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#2

帖子 eexpress »

目标码就是模块??多半还有什么格式要遵循。起码userdrv_其他的函数针对一个模块结构,应该有个标准声明出口。这里只看到module的2个宏出口声明。
胡说的。 :lol:
● 鸣学
flowerfish
帖子: 15
注册时间: 2005-10-15 9:50
来自: HUST

#3

帖子 flowerfish »

可是那个
struct file_operations userdrv_fops={
.owner=THIS_MODULE,
.read=userdrv_read,
.write=userdrv_write,
.open=userdrv_open,
.release=userdrv_close
};
就已经是驱动程序和内核的接口的数据结构了啊,不知道是什么地方出了问题哦
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#4

帖子 eexpress »

struct file_operations userdrv_fops={
在c里面只是一个结构赋值。而且userdrv_fops看了不像一个系统定义的接口宏名字啊。我想多半要看到明显的系统定义的宏接口名才像那么回事。

我只对c熟悉,对模块一窍不通,再次胡说。 :lol:
● 鸣学
回复