当前位置: 技术问答>linux和unix
简单的字符设备驱动程序居然出错!大家帮忙看看哈^_^
来源: 互联网 发布时间:2015-10-11
本文导语: //chardev.c #include #include #include #include #define SUCCESS 0 #define DEVICE_NAME "char_dev" #define BUF_LEN 80 static int Device_Open=0; static char Message[BUF_LEN]; static char *Message_Ptr; static i...
//chardev.c
#include
#include
#include
#include
#define SUCCESS 0
#define DEVICE_NAME "char_dev"
#define BUF_LEN 80
static int Device_Open=0;
static char Message[BUF_LEN];
static char *Message_Ptr;
static int device_open(struct inode *inode,struct file *file)
{
static int counter=0;
#ifdef DEBUG
printk("device_open(%p,%p)n",inode,file);
#endif
if(Device_Open)
return -EBUSY;
Device_Open++;
sprintf(Message,"If I told you once, I told you %d times-Hello,world!n",counter++);
Message_Ptr=Message;
MOD_INC_USE_COUNT;
return SUCCESS;
}
static void device_release(struct inode *inode,struct file *file)
{
#ifdef DEBUG
printk("device_release(%p,%p)n",inode,file);
#endif
Device_Open--;
MOD_DEC_USE_COUNT;
}
static int device_read(struct inode *inode,
struct file *file,
char *buffer,
int length)
{
int bytes_read=0;
#ifdef DEBUG
printk("device_read(%p,%p,%p,%d)n",inode,file,buffer,length);
#endif
if(*Message_Ptr==0)
return 0;
while(length && *Message_Ptr)
{
put_user(*(Message_Ptr++),buffer++);
length--;
bytes_read++;
}
#ifdef DEBUG
printk("Read %d bytes,%d leftn",bytes_read,length);
#endif
return bytes_read;
}
static int device_write(struct inode *inode,
struct file *file,
const char *buffer,
int length)
{
#ifdef DEBUG
printk("device_write(%p,%p,%s,%d)n",inode,file,buffer,length);
#endif
return -EINVAL;
}
static int Major;
struct file_operations Fops={
NULL,
device_read,
device_write,
NULL,
NULL,
NULL,
NULL,
device_open,
device_release
};
int init_module()
{
Major=module_register_chrdev(0,
DEVICE_NAME,
&Fops);
if(Major
#include
#include
#include
#include
#define SUCCESS 0
#define DEVICE_NAME "char_dev"
#define BUF_LEN 80
static int Device_Open=0;
static char Message[BUF_LEN];
static char *Message_Ptr;
static int device_open(struct inode *inode,struct file *file)
{
static int counter=0;
#ifdef DEBUG
printk("device_open(%p,%p)n",inode,file);
#endif
if(Device_Open)
return -EBUSY;
Device_Open++;
sprintf(Message,"If I told you once, I told you %d times-Hello,world!n",counter++);
Message_Ptr=Message;
MOD_INC_USE_COUNT;
return SUCCESS;
}
static void device_release(struct inode *inode,struct file *file)
{
#ifdef DEBUG
printk("device_release(%p,%p)n",inode,file);
#endif
Device_Open--;
MOD_DEC_USE_COUNT;
}
static int device_read(struct inode *inode,
struct file *file,
char *buffer,
int length)
{
int bytes_read=0;
#ifdef DEBUG
printk("device_read(%p,%p,%p,%d)n",inode,file,buffer,length);
#endif
if(*Message_Ptr==0)
return 0;
while(length && *Message_Ptr)
{
put_user(*(Message_Ptr++),buffer++);
length--;
bytes_read++;
}
#ifdef DEBUG
printk("Read %d bytes,%d leftn",bytes_read,length);
#endif
return bytes_read;
}
static int device_write(struct inode *inode,
struct file *file,
const char *buffer,
int length)
{
#ifdef DEBUG
printk("device_write(%p,%p,%s,%d)n",inode,file,buffer,length);
#endif
return -EINVAL;
}
static int Major;
struct file_operations Fops={
NULL,
device_read,
device_write,
NULL,
NULL,
NULL,
NULL,
device_open,
device_release
};
int init_module()
{
Major=module_register_chrdev(0,
DEVICE_NAME,
&Fops);
if(Major